如何使用ng repeat中的选定输入更新我的对象

How can I update my Object with selected inputs in ng-repeat?

本文关键字:输入 更新 我的 对象 何使用 ng repeat      更新时间:2023-09-26

我有这样的代码

{"xyz":[{"a":1,"b":"","c":""}],
"pqr":["l","m"],
"abc":["1234","5678"]}
<div ng-model="item" ng-repeat="product in xyz">
    <div>{product.a}</div>
    <div>
        <select>
            <option ng-repeat="value in pqr">{{value}}</option>
        </select>
    </div>
    <div>
        <select>
            <option ng-repeat="number in abc">{{number}}</option>
        </select>
    </div>
</div>

我想在更改下拉列表中的值时更新我的对象,并更新onject"xyz"!

这样尝试。

您应该为select标记定义模型。

var app = angular.module("app",[]);
app.controller("MyCtrl" , function($scope){
  
    $scope.data = 
        {
      
         "xyz":[{"a":1,"b":"","c":""}],
         "pqr":["l","m"],
         "abc":["1234","5678"]
       }
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   <div ng-repeat="(key,value) in data.xyz">
    <div ng-repeat="(k,v) in value">{{v}}</div>
    <div>
        <select ng-model="model1" ng-options="v1 as v1 for v1 in data.pqr" ng-change="value.b = model1">
        </select>
    </div>
    <div>
        <select ng-model="model2" ng-options="number as number for number in data.abc" ng-change="value.c = model2">
        </select>
    </div>
</div>
</div>

首先不要在<option>上使用ng-repeat,而是使用ng-options。文档和示例可在此处找到:https://docs.angularjs.org/api/ng/directive/ngOptions

至于您的代码,您需要将select绑定到您的对象:

<select ng-model="product.a" ...> //or which ever property you are trying to update