2013-03-15 24 views
0

我的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被剝離掉,服務器是幸福的。]

+1

爲什麼問題中的答案..? – redben 2013-04-19 15:51:50

回答

0

解決:

什麼疼痛的屁股! TGIF因爲現在我有一個原因。

這裏的解決方案:(對不起,我改變了由原來後一些名字)

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

newContainer.GCWRs = selectedGCWRs; 

....然後去和保存newContainer。如果我使用了angular.copy,那麼在ng-repeat中創建的$$ hashkeys不會被剝離掉;如果使用angular.copy,那麼在ng-repeat中創建的$$ hashkeys不會被刪除。服務器會因此拒絕這個集合,因爲它有一個額外的屬性,即$$ hashkey,它與服務器上的模型類不匹配。通過簡單地將集合傳遞給容器,$$ hashkey被剝離,服務器很高興。]

0

我認爲你需要使用

var myGCWRS = []; 
var tmp = angular.forEach($scope.gcwrs, function (gcwr) { 
    if(gcwr.checked) { 
     myGCWRS.push(gcwr); 
    } 
}); 
newContainer.GCWRs = angular.copy(myGCWRS); 
+0

就我測試它而言,這沒有奏效。但我感謝你的幫助。 – Bye 2013-03-15 17:36:51

+0

你幾乎已經有了,「angular.copy」就是問題所在。 – Bye 2013-03-15 20:08:26

0

我真的不知道,你需要一個任何東西的新容器。使用角度的雙向綁定,gcwrs數組始終是最新的。當您準備好處理選定的元素時,只需查看數組。

以下是一個演示一個plnkr:http://plnkr.co/edit/DxQY74XBxzOU66X18xqH

通知對象的狀態變化的複選框被選中/取消選中。

+0

容器是一個服務器端類。它本身與角度無關。 – Bye 2013-03-15 19:39:27

+0

啊,所以,如果你看看plnkr,你可以看到一個函數'$ scope.save()',在這個函數中,你可以使用'$ http'或$ $ resource將檢查過的數據發送到服務器' – Jason 2013-03-15 19:57:06