我正在構建一個CRUD,並且我想要將某個模型鏈接到其他模型。因此,在某些更新/創建模板中,我需要添加其他模型並在提交表單時保存它們的ID。 所以我做了一個由我的後端服務的基本表單。這裏是我嘗試顯示鏈接模型中的一部分:AngularJS:需要將選定的集合附加到模型
<div ng-repeat="item in otherItems">
<select ng-model="item" name="item" >
<option value="1" ng-selected="item">
<option value="2" ng-selected="item">
<option value="3" ng-selected="item">
</select>
<a class="remove" ng-click="removeRelated(item)">remove</a>
</div>
<a ng-click="addRelated()"><i class="icon-plus"></i></a>
注:otherItems可能是在一開始空(做當創建或當一個項目不會鏈接到任何其他相關模型的項目)。 所以,當一個按添加/刪除它會觸發控制器的功能:
$scope.addRelated = function(){
if (typeof $scope[otherItems]=="undefined")
$scope[otherItems] = new Array();
$scope[otherItems].push($scope[otherItems].length);
};
$scope.removeRelated = function (item){
console.debug(item);
var idx = $scope[otherItems].indexOf(item);
if (idx !== -1) {
$scope[otherItems].splice(idx, 1);
}
};
我的問題是,當我救我得到項目中的項目位置(所以它總是0,1,2 ...)我將不會有一個選定ID的數組。 我想我的addRelated可能有問題。我究竟做錯了什麼?
下面是截圖,瞭解的想法,因爲我可能不是很清楚:
你有一個小提琴或一個[plunk](http://plnkr.co)正是你想要做的?上面的代碼看起來像psuedocode。 –
我會盡量在稍後再做一個JsFiddle。但你猜我想。 – Spir