Home How to force a view to load model values prior to loading controller in AngularJS?
Reply: 0

How to force a view to load model values prior to loading controller in AngularJS?

user2579 Published in April 22, 2018, 12:37 am

I am trying to auto post a form from a controller without any user interaction on the form. I have a private init() function in the controller that triggers a button click on the form. But the hidden form fields did not get the values yet. How can I make sure the hidden fields will have values populated before the form submits?

Thank you for any suggestions.

 <form name="myForm" method="post" action="@Model.Settings["URL"]" ng-controller="MyCtrl">

        <input type="hidden" name="userid" value="{{UserSettings.Settings.UserId}}">
        <input type="hidden" name="amount" value="{{Payment.Amount}}">

        <button id="payButton" type="submit" class="action blue"><span class="label">Pay</span></button>

        <script language="javascript">
            var UserSettings = (function (o) {
                return o;

 myControllers.controller('MyCtrl', ['$scope', '$state', '$element', 'dataService', 
  function ($scope, $state, $element, service) {

      $scope.Payment = service.GetPayment());
      $scope.UserSettings = UserSettings;

      function init() {

        // How can I force to have values in the hidden form fields before the button click that submits the form ?



Here is the ui-router states configuration.

var app = angular.module('pay', ['ui.router', 'pay.controllers', 'pay.services', 'exceptionOverride', 'ngSanitize']);

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function ($stateProvider, $urlRouterProvider, $locationProvider) {

        .state('home', {
            url: '/' ,
            templateUrl: 'search'
        .state('payment', {
            url: '/payment',
            templateUrl: 'Payment'

    //setting html5 removes the # from URL but causes routing problems at the moment.

    $urlRouterProvider.rule(function ($injector, $location) {
        //what this function returns will be set as the $location.url
        var path = $location.path(), normalized = path.toLowerCase();
        if (path != normalized) {
        else if (path == '') {
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300726 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO