Angularjs on page load call function

后端 未结 7 471
臣服心动
臣服心动 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:52

    You should call this function from the controller.

    angular.module('App', [])
      .controller('CinemaCtrl', ['$scope', function($scope) {
        myFunction();
      }]);
    

    Even with normal javascript/html your function won't run on page load as all your are doing is defining the function, you never call it. This is really nothing to do with angular, but since you're using angular the above would be the "angular way" to invoke the function.

    Obviously better still declare the function in the controller too.

    Edit: Actually I see your "onload" - that won't get called as angular injects the HTML into the DOM. The html is never "loaded" (or the page is only loaded once).

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