How to execute AngularJS controller function on page load?

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

    Another alternative:

    var myInit = function () {
        //...
    };
    angular.element(document).ready(myInit);
    

    (via https://stackoverflow.com/a/30258904/148412)

    0 讨论(0)
  • 2020-11-22 16:25

    I had the same problem and only this solution worked for me (it runs a function after a complete DOM has been loaded). I use this for scroll to anchor after page has been loaded:

    angular.element(window.document.body).ready(function () {
    
                            // Your function that runs after all DOM is loaded
    
                        });
    
    0 讨论(0)
  • 2020-11-22 16:28

    Try this?

    $scope.$on('$viewContentLoaded', function() {
        //call it here
    });
    
    0 讨论(0)
  • 2020-11-22 16:31

    Yet another alternative if you have a controller just specific to that page:

    (function(){
        //code to run
    }());
    
    0 讨论(0)
  • 2020-11-22 16:31

    Found Dmitry Evseev answer quite useful.

    Case 1 : Using angularJs alone:
    To execute a method on page load, you can use ng-init in the view and declare init method in controller, having said that use of heavier function is not recommended, as per the angular Docs on ng-init:

    This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope.

    HTML:

    <div ng-controller="searchController()">
        <!-- renaming view code here, including the search box and the buttons -->
    </div>
    

    Controller:

    app.controller('SearchCtrl', function(){
    
        var doSearch = function(keyword){
            //Search code here
        }
    
        doSearch($routeParams.searchKeyword);
    })
    

    Warning : Do not use this controller for another view meant for a different intention as it will cause the search method be executed there too.

    Case 2 : Using Ionic:
    The above code will work, just make sure the view cache is disabled in the route.js as:

    route.js

    .state('app', {
        url           : '/search',
        cache         : false, //disable caching of the view here
        templateUrl   : 'templates/search.html'   ,
        controller    : 'SearchCtrl'
      })
    

    Hope this helps

    0 讨论(0)
  • 2020-11-22 16:34

    I could never get $viewContentLoaded to work for me, and ng-init should really only be used in an ng-repeat (according to the documentation), and also calling a function directly in a controller can cause errors if the code relies on an element that hasn't been defined yet.

    This is what I do and it works for me:

    $scope.$on('$routeChangeSuccess', function () {
      // do something
    });
    

    Unless you're using ui-router. Then it's:

    $scope.$on('$stateChangeSuccess', function () {
      // do something
    });
    
    0 讨论(0)
提交回复
热议问题