【发布时间】:2017-05-24 20:23:59
【问题描述】:
使用Controller1 和Controller2 有区别吗?
angular.module('app', [])
.component('foo', {
templateUrl: 'foo.html',
bindings: {
user: '<',
},
controller: Controller1, //Or Controller2
});
function Controller1(){
this.$onInit = function(){
this.user = angular.copy(this.user);
};
this.$onChanges = function(changes){
if(changes.user && !changes.user.isFirstChange()){
this.user = angular.copy(changes.user.currentValue);
}
};
}
function Controller2(){
this.$onChanges = function(changes){
if(changes.user){
this.user = angular.copy(changes.user.currentValue);
}
};
}
既然我可以在 $onChanges 中做同样的事情并保存一些行,我为什么还要打扰 $onInit?
这种类型的初始化在$onChanges 和$onInit 中是否更适合其他类型的初始化?
【问题讨论】:
标签: javascript angularjs angular-components