【发布时间】:2017-02-27 14:19:38
【问题描述】:
我知道您可能会将其标记为重复,但没有一个兄弟主题不能解决我的问题,所以这是我的简单日期倒计时指令:
class Clock {
constructor() {
this.restrict = 'AC';
this.replace = true;
this.template = require('./templates/clock.tpl.html');
this.scope = {};
}
link(scope, elem, attrs, $interval) {
let end = new Date('05/05/2017 9:00 AM');
let _second = 1000;
let _minute = _second * 60;
let _hour = _minute * 60;
let _day = _hour * 24;
scope.showRemaining = () => {
let now = new Date();
let distance = end - now;
let days = Math.floor(distance / _day);
let hours = Math.floor((distance % _day) / _hour);
let minutes = Math.floor((distance % _hour) / _minute);
let seconds = Math.floor((distance % _minute) / _second);
scope.days = days;
scope.hours = hours;
scope.minutes = minutes;
scope.seconds = seconds;
}
$interval(showRemaining, 1000;)
}
}
// create factory function to handle DI
function factory() {
"ngInject";
return new Clock();
}
export default factory;
我一直在搜索这个问题的原因,并且在任何地方我都得到信息,即间隔函数必须作为普通函数传递,不带参数或任何其他插件。但我仍然有同样的错误,即:
TypeError: $interval is not a function
有人可以帮忙吗?
【问题讨论】:
-
通过控制器注入,而不是链接函数
标签: javascript angularjs directive clock