admin管理员组文章数量:1316204
Edit/ update :
I forgot my original code in angular 1.6 (normal way) :
I think, this can helpe you. My job is convert in EcmaScript ES6.
who work great !
How to use $watch
in ES6 Angular controller ?
loginaction(){
this.$scope.$watch('ui.shake', this.resetUiCheck());
....
}
I try this, the result is not expected
resetUiCheck(newValue, oldValue){
console.log(this.ui.shake);
return () => {
alert('foo');
console.log(this);
if(this.ui.shake == true){
this.$timeout(function(){
this.ui.shake = false;
}, 1000);
}
};
}
return always false!
I try this:
this.$scope.$watch('ui.shake', this.resetUiCheck);
And the result is this error
TypeError: Cannot read property 'ui' of undefined
Another question: the $watch
function should not be set in the Contoller constructor?
Edit/ update :
I forgot my original code in angular 1.6 (normal way) :
http://codepen.io/darkiron/pen/qRJmaj
I think, this can helpe you. My job is convert in EcmaScript ES6.
who work great !
How to use $watch
in ES6 Angular controller ?
loginaction(){
this.$scope.$watch('ui.shake', this.resetUiCheck());
....
}
I try this, the result is not expected
resetUiCheck(newValue, oldValue){
console.log(this.ui.shake);
return () => {
alert('foo');
console.log(this);
if(this.ui.shake == true){
this.$timeout(function(){
this.ui.shake = false;
}, 1000);
}
};
}
return always false!
I try this:
this.$scope.$watch('ui.shake', this.resetUiCheck);
And the result is this error
TypeError: Cannot read property 'ui' of undefined
Another question: the $watch
function should not be set in the Contoller constructor?
-
1
1. Why
$watch
at all? 2. Why$timeout
? 3. What do you mean by "return always false"? – a better oliver Commented Mar 13, 2017 at 16:32 - Read How to Debug Small Programs. – georgeawg Commented Mar 14, 2017 at 2:36
- See also AngularJs 1.5 - Component does not support Watchers, what is the work around? – georgeawg Commented Mar 14, 2017 at 2:39
-
@zeroflagL use
$watch
to biding the variable ui.shake (who was boolean) . ` $timeout` for wait the and of css animation Angular is faster than html – darkiron Commented Mar 14, 2017 at 8:12 -
Angular 1.5 offers
$onChanges
, which is preferable to$watch
. Another important point is the reason of change. If e.g. the value is changed by clicking on a button you can listen to that event. As for$timeout
: You can take a look into$animate
that can call functions when the animation ends. Anyway, if you want to keep things as they are you need to replacefunction(){ this.ui.shake = false;
with an arrow function, becausethis
is not the controller here. – a better oliver Commented Mar 14, 2017 at 9:39
3 Answers
Reset to default 7You are directly calling function instead of passing function reference in 2nd parameter.
this.$scope.$watch('ui.shake',this.resetUiCheck.bind(this));
OR
this.$scope.$watch('ui.shake', (newValue, oldValue) =>
this.resetUiCheck( newValue, oldValue)
);
you can also write it in a very simple form
this.$scope.$watch('ui.shake', this.resetUiCheck);
But then you have to write underlying function in Arrow function format
resetUiCheck = (newValue, oldValue) =>
this.resetUiCheck( newValue, oldValue)
);
It should be:
this.$scope.$watch('ui.shake', this.resetUiCheck)
First, never use $watch in controllers. Second, you don't need $watch.
$scope.ui.shake = true;
$timeout(function(){
$scope.ui.shake = false;
}, 1000);
本文标签: javascriptAngular 16 ES6 watchStack Overflow
版权声明:本文标题:javascript - Angular 1.6 ES6 $watch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987330a2408760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论