How to execute AngularJS controller function on page load?

前端 未结 14 2509
一整个雨季
一整个雨季 2020-11-22 16:04

Currently I have an Angular.js page that allows searching and displays results. User clicks on a search result, then clicks back button. I want the search results to be disp

14条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 16:16

    When using $routeProvider you can resolve on .state and bootstrap your service. This is to say, you are going to load Controller and View, only after resolve your Service:

    ui-routes

     .state('nn', {
            url: "/nn",
            templateUrl: "views/home/n.html",
            controller: 'nnCtrl',
            resolve: {
              initialised: function (ourBootstrapService, $q) {
    
                var deferred = $q.defer();
    
                ourBootstrapService.init().then(function(initialised) {
                  deferred.resolve(initialised);
                });
                return deferred.promise;
              }
            }
          })
    

    Service

    function ourBootstrapService() {
    
     function init(){ 
        // this is what we need
     }
    }
    

提交回复
热议问题