javascript - Using $onChanges vs $onInit in angular component -
is there difference between using 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); } }; }
why should bother $oninit
when can same in $onchanges
, save rows?
is type of initialization better in $onchanges
, $oninit
better other kind of initialization?
Comments
Post a Comment