I have a service
我有服务
app.service('myService', function() {
this.list = [];
this.execute = function() {
//this.list is reachable here
angular.forEach(AnArrayHere, function(val, key) {
//this.list is not reachable here
});
}
}
even in controller its accessible
即使在控制器中也可访问
function Ctrl($scope, myService) {
$scope.list = myService.list;
}
Can someone explain me why "this.list" is not reachable within the angular.foreach and how can I access to "this.list" ?
有人可以解释为什么angular.foreach中无法访问“this.list”,我如何访问“this.list”?
2 个解决方案
#1
49
The last parameter in the angular.forEach
(See http://docs.angularjs.org/api/angular.forEach) function is the context for this
. So you'll want something like this:
angular.forEach(参见http://docs.angularjs.org/api/angular.forEach)函数中的最后一个参数是此的上下文。所以你会想要这样的东西:
app.service('myService', function() {
this.list = [];
this.execute = function() {
//this.list is reachable here
angular.forEach(AnArrayHere, function(val, key) {
//access this.list
}, this);
}
}
#2
4
The reason this.list
is not accessible in the foreach is because the context for that function call has changed. It works inside the execute
method as that method is part of the same context as list.
在foreach中无法访问this.list的原因是因为该函数调用的上下文已更改。它在execute方法内部工作,因为该方法是与list相同的上下文的一部分。
Due to all the closures I would assign the this
context to another variable that you can then call later on. Something like this:
由于所有的闭包,我会将此上下文分配给另一个变量,然后您可以调用该变量。像这样的东西:
app.service('myService', function() {
var self = this;
self.list = [];
self.execute = function() {
//this.list and self.list are reachable here
angular.forEach(AnArrayHere, function(val, key) {
//self.this == this.list
});
}
}
This is a slightly more general solution than passing the context as part of the foreach method call and would work in other closure related situations.
这是一种稍微更通用的解决方案,而不是将上下文作为foreach方法调用的一部分传递,并且可以在其他与闭包相关的情况下工作。
#1
49
The last parameter in the angular.forEach
(See http://docs.angularjs.org/api/angular.forEach) function is the context for this
. So you'll want something like this:
angular.forEach(参见http://docs.angularjs.org/api/angular.forEach)函数中的最后一个参数是此的上下文。所以你会想要这样的东西:
app.service('myService', function() {
this.list = [];
this.execute = function() {
//this.list is reachable here
angular.forEach(AnArrayHere, function(val, key) {
//access this.list
}, this);
}
}
#2
4
The reason this.list
is not accessible in the foreach is because the context for that function call has changed. It works inside the execute
method as that method is part of the same context as list.
在foreach中无法访问this.list的原因是因为该函数调用的上下文已更改。它在execute方法内部工作,因为该方法是与list相同的上下文的一部分。
Due to all the closures I would assign the this
context to another variable that you can then call later on. Something like this:
由于所有的闭包,我会将此上下文分配给另一个变量,然后您可以调用该变量。像这样的东西:
app.service('myService', function() {
var self = this;
self.list = [];
self.execute = function() {
//this.list and self.list are reachable here
angular.forEach(AnArrayHere, function(val, key) {
//self.this == this.list
});
}
}
This is a slightly more general solution than passing the context as part of the foreach method call and would work in other closure related situations.
这是一种稍微更通用的解决方案,而不是将上下文作为foreach方法调用的一部分传递,并且可以在其他与闭包相关的情况下工作。