0

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

7
  • Why not do $scope.values[itemStr] = value? Commented May 9, 2014 at 10:04
  • I receive the error Unable to set property 'department' of undefined or null reference Commented May 9, 2014 at 10:10
  • You would need to define the $scope.values object first: $scope.values = {}. Commented May 9, 2014 at 10:12
  • Hello, I tried this and it no longer errors, the data gets set into the model, however the view does not update once the data is set, how do I make the view update to the new model data? Commented May 9, 2014 at 10:18
  • How are you referencing the scoped property in the html? Commented May 9, 2014 at 10:19

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Did you inject the $parse provider in your controller?

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

1 Comment

Doh! No I didn't, I have added the $parse provider now and it successfully parses the variable name. I know this is another question within a question, but once I call the $scope.$apply() method, an error appears: Error: [$rootScope:inprog] $digest already in progress http://errors.angularjs.org/1.2.2/$rootScope/inprog?p0=%24digest Any ideas what this might mean? My view does no update with the correct values because of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.