ng-repeat:如何在select中设置属性倍数

时间:2016-06-17 07:31:07

标签: javascript angularjs

重复时我有对象(arr),他有属性charTypeselectmultiple

我希望如果charType "select"看到正常选择,但如果charType"multiple",请参阅select with multiple。

示例:

如果object(arr)有5个结果0,1,2是charType'select'而3,4 charType是'multiple'。我想看 0选择 1选择 2选择 3选择多个 4选择多个

<div class="form-group" ng-repeat="att in arr track by $index">
  <div class="col-md-3">
    <label for="phone" ng-model="type">{{att.name}}</label>
  </div>
  <div class="col-md-9">
    ({{att.charType}})
    <select class="form-control" ng-model="charObj.charactValue[att.businessassetID + $index]" ng-if="att.charType == 'select' ? 'multiple' : 'multiple=true'">
      <option ng-repeat="itemss in att.value track by $index" value="{{itemss}}">{{itemss}}</option>
    </select>
    <div class="text-center"><a class="insertStore" data-toggle="modal" data-target="#addCharacteristicValue" ng-click="getObj(att)">Добави</a>
    </div>

  </div>
</div>

1 个答案:

答案 0 :(得分:1)

这是一个工作片段:

  • 在选项标记
  • 中使用ng-options代替ng-repeat
  • 按索引删除曲目make select fail
  • 使用简单的ng-if生成两个不同的选择。

似乎没有ng-multiple指令,所以我选择ng-if解决方法。

&#13;
&#13;
angular.module("app", []).controller("ctrl", function($scope){
    $scope.arr = [{charType:'select', name:'select', value:[1, 2]},{charType:'multiple', name:'multiple', value:[1,2]}]; 
    $scope.charObj = {characterValue:{}};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>



<div ng-app="app" ng-controller="ctrl">
    <div class="form-group" ng-repeat="att in arr track by $index">
      <div class="col-md-3">
        <label for="phone" ng-model="type">{{att.name}}</label>
      </div>
      <div class="col-md-9">
        ({{att.charType}})
        <select ng-if="att.charType == 'select'" class="form-control" ng-model="charObj.charactValue[att.businessassetID + $index]"  ng-options="itemss for itemss in att.value">
        </select>
                <select ng-if="att.charType != 'select'" multiple class="form-control" ng-model="charObj.charactValue[att.businessassetID + $index]"  ng-options="itemss for itemss in att.value">
        </select>
      
      </div>
    </div>
    selected : {{charObj.charactValue}} <br />
</div>
&#13;
&#13;
&#13;