zelocalhost
zelocalhost

Reputation: 1183

Dynamic variable name of scope

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 ?

Upvotes: 0

Views: 316

Answers (1)

Jonathan Guerrera
Jonathan Guerrera

Reputation: 991

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

Upvotes: 1

Related Questions