Вопрос

I have an angular controller whereby I'm trying to look through a list of strings, querying an ajax query for each string within the list. The code is as follows:

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

This is the code that doesn't work. it complains with the error '$parse' is undefined. I took this example from a SO post.

I basically want to be able to loop through my itemsliststrings, post the string to an web method, and set the return data for this to a model variable called that particular string. So when I take the "department" string, I submit this string to the web method, I get a data object back, which I then set to the $scope.department object.

Thanks

Это было полезно?

Решение 2

Did you inject the $parse provider in your controller?

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

Другие советы

Have you tried just $scope[itemStr] = data?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top