0

私は CRUD を構築していますが、あるモデルを他のモデルにリンクしたいと考えています。そのため、いくつかの更新/作成テンプレートでは、フォームを送信するときに他のモデルを追加して ID を保存する必要があります。そこで、バックエンドによって提供される基本的なフォームを作成しました。リンクされたモデルを表示しようとする部分は次のとおりです。

<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 は最初は空である可能性があります (作成時、またはアイテムが他の関連モデルのアイテムにリンクされていない場合)。したがって、追加/削除を 1 回押すと、コントローラーの機能がトリガーされます。

$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に何か問題があると思います。私は何を間違っていますか?

あまり明確ではないかもしれないので、アイデアを理解するためのスクリーンショットを次に示します。 ここに画像の説明を入力

4

1 に答える 1

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

あなたが求めているものから外れている場合は申し訳ありません...質問が少し曖昧だったので、機能の一部を推測しています。

于 2012-10-26T18:06:21.700 に答える