Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  58] [ 5]  / answers: 1 / hits: 21159  / 12 Years ago, thu, february 21, 2013, 12:00:00

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 ?


More From » foreach

 Answers
15

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:



app.service('myService', function() {

this.list = [];

this.execute = function() {

//this.list is reachable here

angular.forEach(AnArrayHere, function(val, key) {
//access this.list
}, this);

}
}

[#80070] Wednesday, February 20, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
skylerselenem

Total Points: 282
Total Questions: 101
Total Answers: 107

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;