【发布时间】:2017-08-10 08:38:00
【问题描述】:
我有一个简单的组件,它使用计时器来增加视图中的值。我使用 setInterval 来自动增加值,并使用按钮来增加值。单击按钮按预期工作,但 setInterval 什么都不做,没有错误,没有增量。代码如下:
export default angular.module('directives.timer', [])
.component('timer',{
bindings:{
count: '<'
},
template:`<div>{{$ctrl.count}}</div>
<div><button ng-click="$ctrl.increment()">increment</button></div>
<pre>{{$ctrl}}</pre>`,
controller: function(){
this.count = 0;
this.tick = function(){
this.count = this.count++;
}
this.increment = function(){
this.count++;
}
this.$onInit = function(){
var _this = this;
setInterval(function(){
_this.tick();
}, 1000);
}
}
}).name
tick 函数被调用,值增加,但 UI 没有更新。 怎么了?谢谢
【问题讨论】:
-
我认为这与单向绑定没有任何关系。您可以将绑定更改为 {},这仍然有效。您的控制器所做的第一件事就是手动将 count 的本地属性设置为 0。
标签: javascript angularjs angular-components