编辑/更新:
我忘记了 angular 1.6 中的原始代码(正常方式):
http://codepen.io/darkiron/pen/qRJmaj
我想,这可以帮助你。我的工作是在 EcmaScript ES6 中进行转换。
谁的工作很棒!
如何$watch
在 ES6 Angular 控制器中使用?
loginaction(){
this.$scope.$watch('ui.shake', this.resetUiCheck());
....
}
我试试这个,结果不是预期的
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);
}
};
}
返回总是假的!
我试试这个:
this.$scope.$watch('ui.shake', this.resetUiCheck);
结果就是这个错误
TypeError:无法读取未定义的属性“ui”
还有一个问题:$watch
Contoller构造函数中不应该设置函数吗?