在我的AngularJS應用程序中,我必須初始化一些onload值。用init()調用或ng-init初始化數據?
我在這方面寫了一個函數init()
。
function init() {
this.var = 'example';
}
它是更好調用init
功能在我的JS腳本的末尾,或與ng-init
屬性?
解決方案1
/* script.js */
function init() { /* instructions */ }
init();
解決方案2
/* script.js */
function init(){ // instructions // }}
// Not calling init() here
<!-- file.html -->
<div ng-controller="exampleCtrl as example" ng-init="example.init()">
解決方案1比'ng-init'好# – Akashii
您能解釋**爲什麼**嗎? – Zooly
https://docs.angularjs.org/api/ng/directive/ngInit。你可以在文檔中看到它 – Akashii