Есть ли разница между использованием Controller1
vs 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);
}
};
}
Почему я должен беспокоиться $onInit
, когда я могу просто сделать то же самое в этом $onChanges
и сохранить несколько строк?
Этот тип инициализации лучше в $onChanges
и $onInit
лучше для какой-либо другой инициализации?