Angular使用动态名称访问$ scope对象

时间:2014-05-09 10:00:41

标签: javascript jquery angularjs angularjs-scope

我有一个角度控制器,我试图查看字符串列表,查询列表中每个字符串的ajax查询。代码如下:

var itemsliststrings = ["department", "year", "project", "subdepartment"];

itemsliststrings.forEach(function (itemStr) {
    $http.post("/Budget/GetListBudget", { budgetType: itemStr })
    .success(function (data) {
        var the_string = itemStr;
        var model = $parse(the_string);
        model.assign($scope, data);
        $scope.$apply();
    })
    .error(function (error) {
        console.error(error);
        toastr.error('An error occured, unable to load ' + itemStr);
    });
});

这是不起作用的代码。它抱怨错误'$parse' is undefined。我从SO帖子中拿了这个例子。

我基本上希望能够遍历我的itemsliststrings,将字符串发布到Web方法,并将此返回数据设置为名为该特定字符串的模型变量。所以,当我选择"部门"字符串,我将此字符串提交给Web方法,我得到一个数据对象,然后我将其设置为$ scope.department对象。

由于

2 个答案:

答案 0 :(得分:2)

您是否尝试过$ scope [itemStr] =数据?

答案 1 :(得分:1)

您是否在控制器中注入了$ parse提供程序?

    .controller('yourController',function($scope,$parse,etc..){});