2012-10-26 2 views
0

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는 처음에 비어있을 수 있습니다 (일을 할 때 만들거나 항목이 기타 관련 모델의 항목에 연결되지 않은 경우). 나는 내가 (그래서 항상 0, 1, 2, ...의) 항목에서 항목의 위치를 ​​얻을 저장할 때

$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); 
    } 
}; 

내 문제가 하나 개를 눌러 추가 할 때 그래서/그것은 컨트롤러의 기능을 트리거 제거 선택한 ID 배열이 없습니다. 아마도 내 addRelated에 문제가있는 것 같군요. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 여기

내가 아주 명확하지 않을 수 있기 때문에 생각을 이해하는 스크린 샷입니다 :이 같은 이 enter image description here

+0

정확히 무엇을하려는 것입니까? [plunk] (http://plnkr.co)가 있습니까? 위의 코드는 psuedocode처럼 보입니다. –

+0

나중에 JsFiddle을하려고 노력할 것입니다. 그러나 당신은 내가 생각하는 아이디어를 얻는다. – Spir

답변

1

그래서 뭔가? http://plnkr.co/edit/6eqWL5

마크 업

..

<body ng-controller="MainCtrl"> 
    <div ng-repeat="otherItem in otherItems"> 
    <select ng-model="otherItem.selectedItem" ng-options="item for item in otherItem.items"></select> 
    <a ng-click="removeOtherItem($index)">remove</a> 
    </div> 
    <a ng-click="addOtherItem()">add</a> 
    <hr/> 
    Selected: 
    <ul> 
    <li ng-repeat="otherItem in otherItems"> 
     otherItem[{{$index}}].selectedItem = {{otherItem.selectedItem}} 
    </li> 
    </ul> 
</body> 

조금 애매 당신이 ... 질문을 요청하고으로부터의 경우 코드

app.controller('MainCtrl', function($scope) { 
    $scope.otherItems = [ 
     { 
     selectedItem: null, 
     items: [1, 2, 3] 
     }, 
     { 
     selectedItem: null, 
     items: [4, 5, 6] 
     } 
    ]; 
    $scope.addOtherItem = function(){ 
    $scope.otherItems.push({ items: [7, 8, 9]}); 
    }; 
    $scope.removeOtherItem = function(index) { 
    $scope.otherItems.splice(index, 1); 
    }; 
}); 

죄송합니다, 그래서 난 일부 기능을 추측합니다.

+0

막연한 것에 대해 매우 유감스럽게 생각합니다. 내 영어가 나쁘다. 드롭 다운은 이미 백엔드 코드에서 빌드됩니다. 따라서 모든 추가 드롭 다운은 동일합니다 ("동일한 모델"목록). 예 : 사용자를 편집하여 해당 사용자가 0에 연결된 그룹을 만들 수 있습니다. 따라서 사용자를 편집 할 때 그룹에 연결할 수 있습니다. 사용자가 새 드롭 다운을 추가하고 일치시킬 그룹을 선택하도록하면됩니다. 그런 다음 양식을 제출할 때마다 모든 그룹 ID가 그룹이라는 배열로 전송됩니다. 내 실제 예제에서는 모든 모델에 대한 코드를 사용하여 CRUD가 될 것입니다. (그 이유는 내가 모호한 이유를 설명 할 수 있습니다) – Spir

+0

아마도 템플릿으로 작업해야합니다. '