angular - DoCheck in component do detect changes in a service -
i had problem of "how detect change in service, , make visible in component".
i solved using docheck() :
docheck(){ if( this.componentvar != this.myservice.servicevar){ this.componentvar = this.myservice.servicevar; } } and works well! didn't see anywhere method, question: think solution one?
$docheck considered way of "watching" replace $scope watch. can see code bind component service variable. close coupling , not recommended. runs @ every digest cycle causing performant issues. better way use 1 way binding on component variable. component become dump component more reusable.
var childcomponent = { bindings: { componentvar: '<' }, controller: function () { this.$onchanges = function (changes) { if(changes.componentvar){ //do here } }; } }; angular .module('app') .component('childcomponent', childcomponent);
Comments
Post a Comment