我正在尝试创建一个附加了id的变量列表。我在foreach循环中使用以下代码。
angular.forEach(str, function(value, key) {
console.log(key + ': ' + value.id);
//we found project id
//now attach it to dynamic variable
$http.get("http://abounde.com/portal/api/tasks/"+value.id).then(function(response) {
//create dynamic variable
if (key != null) {
$parse("project_"+value.id).assign($scope, value);
}
$scope.value = response.data.taskLists;
});
});
我希望将project_1,project_2,project_3 ....传递给范围。
我做错了什么?
编辑:
看了一些建议后试了一下..
app.controller('portalController',function($scope, $http, $rootScope){
$scope.$on('$viewContentLoaded', function() {
$rootScope.backgroundImg="url(" + images[2].imgObj.src + ")"; //abt bg
$http.get("http://abounde.com/portal/api/projects").then(function(response) {
$scope.myData = response.data.projects;
var x=0;
var str = response.data.projects;
angular.forEach(str, function(value, key) {
console.log(key + ': ' + value.id);
//we found project id
//now attach it to dynamic variable
$scope.projects = [];
$http.get("http://abounde.com/portal/api/tasks/"+value.id).then(function(response) {
//create dynamic variable
$scope.projects.push(value.id) = response.data.tasklists;
});
//console.log($scope.projects);
});
});
});
//make a function which takes project id as parameter and returns tasks list
$scope.getTasksList = function(projectId)
{
$http.get("http://abounde.com/portal/api/tasks/"+projectId).then(function(response) {
$scope.myTasks = response.data.tasklists;
});
}
});
以下错误
如何在视图上访问动态变量?我假设我需要使用类似的东西..
以角度访问动态变量的实际方法是什么?
答案 0 :(得分:2)
无需使用$parse
将其更改为$ scope,并使用正确的对象maniuplation。
$scope['project_'+value.id] = value;
或者,您可以使用动态值数组中的数据;
$scope.projects = [];
// inside your response
$scope.projects.push({
_id : 'project_'+value.id,
value : value
});
HTML:
<div ng-repeat="project in projects">
id: {{ project._id}}
value: {{ project.value}}
</div>