0

我的HTML:

 <ol  ng-model="gcwr.checked" ng-repeat="gcwr in gcwrs" id="gcwr">                         
 <label class="checkbox" for="{{gcwr.Id}}">  
 {{gcwr.Description}}
 <input ng-model="gcwr.checked" type="checkbox"  /></label>
 </ol>   

我的容器控制器:

   // initialize section
    $scope.gcwrs = API.GetGCWRs();

   // in the save/submit function
    var newContainer = $scope.newContainer;
    var myGCWRS;
    var tmp = angular.forEach($scope.gcwrs, function (gcwr) {
        myGCWRS += gcwr.checked ? gcwr : null;
        });

    newContainer.GCWRs = [
        angular.copy(myGCWRS)
    ];

问题:

GCWR 填充在表单中,但我在提交/保存函数广告中收集检查的 gcwrs 时做错了,将集合添加到 newContainer。

有任何想法吗?

-- 在 Angular 的世界里永远不会有无聊的一天 :(

解决了:

好痛啊!!!TGIF 因为现在我有一个理由。

这是解决方案:(对不起,我从原始帖子中更改了一些名称)

    var selectedGCWRs = [];        
    var tmp = angular.forEach($scope.gcwrs, function (gcwr) {            
        if (gcwr.checked) {      
            selectedGCWRs.push(gcwr);
        }
    });

    newContainer.GCWRs = selectedGCWRs;

   .... then go on and save the newContainer.

[注意:如果我使用了 angular.copy,Angular 在 ng-repeat 中创建的 $$hashkeys 就不会被剥离;因此,服务器将拒绝此集合,因为它有一个额外的属性,即 $$hashkey,它与服务器上的模型类不匹配。通过简单地将集合传递给容器,$$hashkeys 被剥离并且服务器很高兴。]

4

3 回答 3

0

我认为你需要使用

var myGCWRS = [];
var tmp = angular.forEach($scope.gcwrs, function (gcwr) {
    if(gcwr.checked) {
        myGCWRS.push(gcwr);
    }
});
newContainer.GCWRs = angular.copy(myGCWRS);
于 2013-03-15T16:15:06.390 回答
0

我不确定您是否需要一个新容器来存放任何东西。通过 angular 的双向绑定,gcwrs 数组始终是最新的。当您准备好处理选定的元素时,只需查看数组即可。

这是一个演示的 plnkr:http: //plnkr.co/edit/DxQY74XBxzOU66X18xqH

请注意,当复选框被选中/取消选中时,对象状态会发生变化。

于 2013-03-15T19:07:20.170 回答
0

解决了:

好痛啊!!!TGIF 因为现在我有一个理由。

这是解决方案:(对不起,我从原始帖子中更改了一些名称)

var selectedGCWRs = [];        
var tmp = angular.forEach($scope.gcwrs, function (gcwr) {            
    if (gcwr.checked) {      
        selectedGCWRs.push(gcwr);
    }
});

newContainer.GCWRs = selectedGCWRs;

.... 然后继续保存 newContainer。

[注意:如果我使用了 angular.copy,Angular 在 ng-repeat 中创建的 $$hashkeys 就不会被剥离;因此,服务器将拒绝此集合,因为它有一个额外的属性,即 $$hashkey,它与服务器上的模型类不匹配。通过简单地将集合传递给容器,$$hashkeys 被剥离并且服务器很高兴。]

于 2013-03-15T20:09:05.977 回答