<分区>
在 AngularJS 中,如果我可以在没有该函数的情况下进行相同的初始化,那么 $onInit
函数的目的是什么?
例如:
module.component("myComponent", {
...
controller: function() {
const $ctrl = this;
$ctrl.$onInit = () => {
$ctrl.var1 = "hello";
$ctrl.var2 = { test: 3 };
}
}
});
也可以这样做:
module.component("myComponent", {
...
controller: function() {
const $ctrl = this;
$ctrl.var1 = "hello";
$ctrl.var2 = { test: 3 };
}
});
有没有需要 $onInit
的情况?