Angularjs on page load call function

后端 未结 7 470
臣服心动
臣服心动 2021-01-14 10:09

I am learning AngularJS. I have some article tag and on clicking on a button each article page is showed without any page refresh. This is one page website. Wha

相关标签:
7条回答
  • 2021-01-14 10:30
        var someVr= element[0].querySelector('#showSelector');
            myfunction(){
            alert("hi");
            }   
            angular.element(someVr).ready(function () {
               myfunction();
                });
    

    This will do the job.

    0 讨论(0)
  • 2021-01-14 10:32

    you can also use the below code.

    function activateController(){
         console.log('HELLO WORLD');
    }
    
    $scope.$on('$viewContentLoaded', function ($evt, data) {
        activateController();
    });
    
    0 讨论(0)
  • 2021-01-14 10:37
    <section ng-controller="testController as ctrl" class="test_cls"  data-ng-init="fn_load()">
    
    $scope.fn_load = function () {
      console.log("page load")
    };
    
    0 讨论(0)
  • 2021-01-14 10:40

    you can use it directly with $scope instance

         $scope.init=function()
            {
                console.log("entered");
                data={};
                /*do whatever you want such as initialising scope variable,
                  using $http instance etcc..*/
            }
           //simple call init function on controller
           $scope.init();
    
    0 讨论(0)
  • 2021-01-14 10:43

    Instead of using onload, use Angular's ng-init.

    <article id="showSelector" ng-controller="CinemaCtrl" ng-init="myFunction()">

    Note: This requires that myFunction is a property of the CinemaCtrl scope.

    0 讨论(0)
  • 2021-01-14 10:50

    It's not the angular way, remove the function from html body and use it in controller, or use

    angular.element(document).ready
    

    More details are available here: https://stackoverflow.com/a/18646795/4301583

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