variable is not accessible in angular.forEach

后端 未结 2 1681
慢半拍i
慢半拍i 2021-01-01 13:27

I have a service

app.service(\'myService\', function() {
    this.list = [];
    this.execute = function() {
        //this.list is reachable here
        a         


        
2条回答
  •  时光说笑
    2021-01-01 13:53

    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);
    
        }
    }
    

提交回复
热议问题