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

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

Jyina
1#
Jyina Published in 2017-12-07 16:17:42Z

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.

<div>
 <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;
            })(@Html.Raw(JsonConvert.SerializeObject(@Model)));
        </script>
    </form>
</div>

 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 ?

          $element.find('#payButton').trigger("click");
      };

      init();
  }]);

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) {

    $stateProvider
        .state('home', {
            url: '/' ,
            templateUrl: 'search'
        })
        .state('payment', {
            url: '/payment',
            templateUrl: 'Payment'
        });

    //setting html5 removes the # from URL but causes routing problems at the moment.
    //$locationProvider.html5Mode(true);

    $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) {
            $location.replace().path(normalized);
        }
        else if (path == '') {
            $location.path('/');
        }
    });
}]);
lzagkaretos
2#
lzagkaretos Reply to 2017-12-07 19:16:23Z

You can put the populations in the point you define the state which has MyCtrl as follows:

.state('myState', {
    // ...
    resolve: {
         Payment: ['dataService', function (dataService) {
             return dataService.GetPayment();
         }],
         // Same for other objects needed
    }
})

And then in your Controller:

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

      $scope.Payment = Payment;
      // Same for other objects needed

      // Rest code         
}

The code in the Controller would not start running before all actions and promises in the resolve section finish.

Update:

You can put init call inside a $timeout.

$timeout(function(){
    init();
});

Of course, you have to inject $timeout in Controller function along with the other dependencies.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO