简体   繁体   English

具有两个值的angularjs ngmodel值

[英]angularjs ngmodel value with two values

I have this dropdown control 我有这个下拉控件

<div ng-repeat="prop in props" style="margin-bottom: 10px;">
  <label class="col-md-4 control-label">Property {{$index + 1}} ({{prop.Value}})</label>
  <div class="col-md-8">
    <select ng-model="gradingvalues[$index]" class="form-control">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
    </select>
  </div>

And in the js code the gradingvalues array is declared as: 在js代码中,gradingvalues数组声明为:

$scope.gradingvalues = [];

What I want is, beside the actual value which is selected in the dropdown, I want to have the propertyID as well, something like: 我想要的是,除了在下拉列表中选择的实际值之外,我还想要有propertyID,例如:

ng-model="gradingvalues[$index, propertyID]"

Is that possible? 那可能吗?

Change your $scope.gradingvalues = []; 更改$scope.gradingvalues = []; instead of an array use an object: 代替数组使用对象:

$scope.gradingvalues = {};

And update your ng-model 并更新您的ng-model

ng-model="gradingvalues[prop]"

This way you will have the " propertyID " as a key with the value selected on the select element. 这样,您将拥有“ propertyID ”作为键,并具有在选择元素上选择的值。

Props and gradingvalues will be always in a 1:1 relation so you can use a property of "prop" to store the grading values: 道具和等级值始终保持1:1关系,因此您可以使用属性“ prop”来存储等级值:

<div ng-repeat="prop in props" style="margin-bottom: 10px;">
  <label class="col-md-4 control-label">Property {{$index + 1}}    ({{prop.Value}})</label>
  <div class="col-md-8">
    <select ng-model="prop.gradingvalue" class="form-control">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
    </select>
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM