我的HTML:AngularJs - 結合複選框集合中控制器
<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)
];
問題:
的GCWRs被填充在表單,但我在收集章節時做錯了事ecked gcwrs在提交/保存功能ad中添加collection到newContainer。
任何想法?
- 不要在一個平淡的日子中的角世界:(
解決:
什麼疼痛的屁股!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,那麼在ng-repeat中創建的$$ hashkeys不會被剝離;服務器會因此拒絕這個集合,因爲它有一個額外的屬性,即$$ hashkey,它與服務器上的模型類不匹配。通過傳遞收集到容器中,$$ HASHKEYS被剝離掉,服務器是幸福的。]
爲什麼問題中的答案..? – redben 2013-04-19 15:51:50