2014-11-21 78 views
1

我有一个json对象,通过ng-repeat中的复选框加载。它列出了可能的服务,我试图访问我提交表单时已经检查过的服务。从复选框循环中循环浏览json对象

我加载的复选框通过:

<label style="margin-right:10px" ng-repeat="item in dsServces"> 
    <input 
     type="checkbox" 
     name="selectedFruits" 
     value="{{item.DefaultServiceID}}" 
     ng-model="expense.dsService[item.DefaultServiceName]" 
    > {{item.DefaultServiceName}} 
</label> 

我可以看到费用是由与测试内置:

{{ expense.dsService }} 

回报(当一对夫妇被选中)

{"Email":true,"Backups":true} 

但我不能解决如何循环,所以它只是说服务的名称,所以我可以添加它们反对客户托梅尔。

在后端我有:

$scope.expense = { 
    tags: [] 
}; 

而且在提交:

angular.forEach($scope.expense, function(item) { 
    console.log(item); 
}); 

哪个吐出Object {Email: true, Backups: true},但我不能工作了有一个循环,它只是返回其任何事物都是真实的。

我的主要目的是让像

angular.forEach(forloop) { 
    //service.item should be like 'Email' whatever the item.DefaultServiceName is 
    // the loop should be all items that are checked 
    addService(service.item); 
}); 
+0

不清楚。创建小提琴,以便人们可以帮助你。 – 2014-11-21 14:01:57

回答

0

如果我正确理解,你要的对象键值推到一个数组。如果是这样的话,只是稍作改变你forEach()

像这样:

// The first parameter is what you want to loop through 
// Second is the iterator, you can separate out the key and values here 
// The third is the context, since I put the array we want to push to in here 
// we just need to call `this.push()` within the iterator 
angular.forEach($scope.expense, function(value, key) { 
    this.push(key); 
}, $scope.expense.tags); 

让我知道,如果这有助于。