I'm trying to push an item to an array every 5 seconds in an AngularJS service, but I keep getting 'items is not defined' after 1 item is added dynamically...
我试图在AngularJS服务中每隔5秒将一个项目推送到一个数组,但是在动态添加一个项目之后我一直得到'项目未定义'...
I've been playing with this simple code for over an hour now, I'm sure it's something silly but I can't figure it out. :/
我已经玩了这个简单的代码一个多小时了,我确定这是愚蠢的但是我无法弄明白。 :/
app.service('ItemService', function() {
var items = [9000];
this.addItem = function () {
items.push(Math.random());
}
setInterval(this.addItem(), 5000);
return {
get: function() {
return items;
}
}
});
Plnkr: http://plnkr.co/edit/EVFUww7dfUAJzQqth7mx
Plnkr:http://plnkr.co/edit/EVFUww7dfUAJzQqth7mx
4 个解决方案
#1
3
You're using angular, so you shouldn't be doing setInterval
, but rather use the $interval
service provided by Angular.
你正在使用angular,所以你不应该使用setInterval,而是使用Angular提供的$ interval服务。
That, coupled with the correct observation of @techfoobar, would result in the correct behavior you are looking for:
再加上对@techfoobar的正确观察,将导致您正在寻找的正确行为:
app.service('ItemService', ['$interval', function($interval) {
var items = [9000];
this.addItem = function () {
items.push(Math.random());
}
$interval(this.addItem, 5000);
return {
get: function() {
return items;
}
}
}]);
工作的Plunker。
#2
3
You are calling addItem()
and passing its return value to setInterval()
- instead of the function itself.
您正在调用addItem()并将其返回值传递给setInterval() - 而不是函数本身。
It should be:
它应该是:
setInterval(this.addItem, 5000); // no () after addItem
#3
0
try using $interval() service instead of setInterval
尝试使用$ interval()服务而不是setInterval
$interval($scope.item, "time_interval");
#4
0
Timing function like window.setInterval, in angularjs are wrapped in $interval (https://docs.angularjs.org/api/ng/service/$interval). Angular manages interval function like a promise to ensure more control over timer.
与angularjs中的window.setInterval之类的定时函数包含在$ interval(https://docs.angularjs.org/api/ng/service/$interval)中。 Angular管理间隔函数就像一个承诺,以确保更多地控制计时器。
#1
3
You're using angular, so you shouldn't be doing setInterval
, but rather use the $interval
service provided by Angular.
你正在使用angular,所以你不应该使用setInterval,而是使用Angular提供的$ interval服务。
That, coupled with the correct observation of @techfoobar, would result in the correct behavior you are looking for:
再加上对@techfoobar的正确观察,将导致您正在寻找的正确行为:
app.service('ItemService', ['$interval', function($interval) {
var items = [9000];
this.addItem = function () {
items.push(Math.random());
}
$interval(this.addItem, 5000);
return {
get: function() {
return items;
}
}
}]);
工作的Plunker。
#2
3
You are calling addItem()
and passing its return value to setInterval()
- instead of the function itself.
您正在调用addItem()并将其返回值传递给setInterval() - 而不是函数本身。
It should be:
它应该是:
setInterval(this.addItem, 5000); // no () after addItem
#3
0
try using $interval() service instead of setInterval
尝试使用$ interval()服务而不是setInterval
$interval($scope.item, "time_interval");
#4
0
Timing function like window.setInterval, in angularjs are wrapped in $interval (https://docs.angularjs.org/api/ng/service/$interval). Angular manages interval function like a promise to ensure more control over timer.
与angularjs中的window.setInterval之类的定时函数包含在$ interval(https://docs.angularjs.org/api/ng/service/$interval)中。 Angular管理间隔函数就像一个承诺,以确保更多地控制计时器。