angularjs 1.5 组件单向绑定不适用于 setInterval

angularjs 1.5 component one way binding not working with setInterval

我有一个简单的组件,它使用计时器来增加视图中的值。我使用 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 没有更新。 怎么了?谢谢

Angular 不知道发生了更改,因为您正在更改 angular 范围之外的值。而是使用 Angulars 包装器 $interval。

Docs:

AngularJS's wrapper for window.setInterval. The fn function is executed every delay milliseconds.