Difference between onLoad and ng-init in angular

后端 未结 3 1011
失恋的感觉
失恋的感觉 2020-12-09 07:49

I am learning angular. I don\'t understand what is difference between onLoad and ng-init for initialization of a variable. In which scope it creates this variable.

<

3条回答
  •  囚心锁ツ
    2020-12-09 08:10

    From angular's documentation,

    ng-init SHOULD NOT be used for any initialization. It should be used only for aliasing. https://docs.angularjs.org/api/ng/directive/ngInit

    onload should be used if any expression needs to be evaluated after a partial view is loaded (by ng-include). https://docs.angularjs.org/api/ng/directive/ngInclude

    The major difference between them is when used with ng-include.

    In this case, myFunction is called everytime the partial view is loaded.

    Whereas, in this case, myFunction is called only once when the parent view is loaded.

提交回复
热议问题