我尝试在 angularjs foreach 中定义动态变量名称,如下所示:
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;
});
});
所以,它不起作用,我遇到了这个错误:无法设置未定义的属性“feed1”
正确的语法是什么?
最佳答案
您的 $scope 对象是否有 window
属性?如果是,请遵循马克西姆的答案。如果没有,请尝试以下操作:
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;
});
});
编辑:
工作plunkr:http://plnkr.co/edit/eVLJKIspLwKPCYAg7L8w?p=preview
关于javascript - 作用域的动态变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26430640/