0

I try to define a dynamic variable name in a angularjs foreach, like this :

angular.forEach($scope.posts, function (item) {
    // counter increment
    counter++;

    var idPage = 'feed' + counter;

    FeedService.parseFeed(url).then(function(res) {
        $scope.window[idPage] = res.data.responseData.feed.entries;
    });

});

so, it's doesn't work and i have this error : Cannot set property 'feed1' of undefined

what's the correct syntax ?

2
  • Could you provide a plnkr? Commented Oct 17, 2014 at 18:19
  • $scope['feed1'] = 'test'; console.log($scope.feed1); Commented Oct 17, 2014 at 19:10

1 Answer 1

1

Does your $scope object have a window property? If yes, go with Maxim's answer. If not, try this:

angular.forEach($scope.posts, function (item) {
    // counter increment
    counter++;

    var idPage = 'feed' + counter;

    FeedService.parseFeed(url).then(function(res) {
        $scope[idPage] = res.data.responseData.feed.entries;
    });

});

Edit:

Working plunkr: http://plnkr.co/edit/eVLJKIspLwKPCYAg7L8w?p=preview

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

Comments

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.