Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS:需要将选定集合附加到模型_Angularjs - Fatal编程技术网

AngularJS:需要将选定集合附加到模型

AngularJS:需要将选定集合附加到模型,angularjs,Angularjs,我正在构建一个CRUD,我想将一些模型链接到其他模型。因此,在一些更新/创建模板上,我需要添加一些其他模型,并在提交表单时保存它们的id。 所以我做了一个基本的表单,由我的后端提供服务。以下是我尝试显示链接模型的部分: <div ng-repeat="item in otherItems"> <select ng-model="item" name="item" > <option value="1" ng-selected="item"&g

我正在构建一个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>
我的问题是,当我保存时,我得到了项目在项目中的位置(因此它总是0、1、2…),我不会有一个选定ID的数组。 我想我的电脑可能有问题。我做错了什么

下面是一个屏幕截图来理解这个想法,因为我可能不太清楚:

像这样的事情

标记

<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>

对不起,如果你的要求不符合。。。这个问题有点含糊不清,所以我在猜测一些功能。

你对你想做的事情有什么了解吗?上面的代码看起来像psuedocode。稍后我将尝试执行JSFIDLE。但我想你明白了。很抱歉说得含糊不清。我的英语不好。下拉列表已经从我的后端代码生成。因此,每个添加的下拉列表都是相同的(“相同型号”列表)。示例:您可以编辑一个用户,该用户链接到0到任何组。因此,编辑用户时,可以将其链接到组。我想我会让用户添加一个新的下拉列表并选择要匹配的组。然后,在提交表单时,每个组id都会发送到一个名为group的数组中。在我的真实示例中,我正在尝试使其干燥,因此我将使用任何模型的代码作为CRUD(这可能解释我含糊不清的原因),也许我应该使用一个模板:
相关项:
并制定一个指令?如果您正在创建一个Angular应用程序,我建议不要使用服务器将数据呈现为html。使用Angular,然后让它通过JSON从服务器加载数据。服务器只进行下拉列表。我的服务器已满。我正在使用一个API来破坏我的模型。
<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);
  };
});