变量未被angular.forEach访问 [英] variable is not accessible in angular.forEach

查看:143
本文介绍了变量未被angular.forEach访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务

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

有人能解释我为什么this.list不是angular.foreach内到达,我怎样才能获得this.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 (见<一href=\"http://docs.angularjs.org/api/angular.forEach\">http://docs.angularjs.org/api/angular.forEach)功能是为上下文这个所以,你想是这样的:

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

    }
}

这篇关于变量未被angular.forEach访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆