How to execute AngularJS controller function on page load?

前端 未结 14 2313
一整个雨季
一整个雨季 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:13

    On the one hand as @Mark-Rajcok said you can just get away with private inner function:

    // at the bottom of your controller
    var init = function () {
       // check if there is query in url
       // and fire search in case its value is not empty
    };
    // and fire it after definition
    init();
    

    Also you can take a look at ng-init directive. Implementation will be much like:

    // register controller in html
    <div data-ng-controller="myCtrl" data-ng-init="init()"></div>
    
    // in controller
    $scope.init = function () {
        // check if there is query in url
        // and fire search in case its value is not empty
    };
    

    But take care about it as angular documentation implies (since v1.2) to NOT use ng-init for that. However imo it depends on architecture of your app.

    I used ng-init when I wanted to pass a value from back-end into angular app:

    <div data-ng-controller="myCtrl" data-ng-init="init('%some_backend_value%')"></div>
    
    0 讨论(0)
  • 2020-11-22 16:13

    You can do this if you want to watch the viewContentLoaded DOM object to change and then do something. using $scope.$on works too but differently especially when you have one page mode on your routing.

     $scope.$watch('$viewContentLoaded', function(){
        // do something
     });
    
    0 讨论(0)
  • 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
     }
    }
    
    0 讨论(0)
  • 2020-11-22 16:17

    call initial methods inside self initialize function.

    (function initController() {
    
        // do your initialize here
    
    })();
    
    0 讨论(0)
  • 2020-11-22 16:25
    angular.element(document).ready(function () {
    
        // your code here
    
    });
    
    0 讨论(0)
  • 2020-11-22 16:25

    Dimitri's/Mark's solution didn't work for me but using the $timeout function seems to work well to ensure your code only runs after the markup is rendered.

    # Your controller, including $timeout
    
    var $scope.init = function(){
     //your code
    }
    
    $timeout($scope.init)
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题