AngularJs ng选项重复成重复

时间:2019-03-06 09:00:18

标签: angularjs angularjs-directive

具有代码:

<tr data-ng-repeat="item in data.items">
     <td>{{item.Id}}</td>
     <td>
         <div>
             {{ data.items[$index].selectedBusinessType.id }}
             {{ data.items[$index].businessTypes[$index].id }}
             <select ng-options="businessType.name for businessType 
                     in item.businessTypes track by businessType.id"
                     data-ng-model="item.businessTypes.id == item.selectedBusinessType.id">
             </select>
         </div>
     </td>
 </tr>

在div模型中,结果还可以,但下拉菜单仍然无效

没有值的下拉屏幕

来自后方的范围数据

我可以从我做错的事情中得到解决方案吗?

2 个答案:

答案 0 :(得分:0)

您可能需要过滤器。尝试类似的操作:(插件:https://next.plnkr.co/edit/DiInzWMC2k8WZzuH

<select class="form-control" ng-model="$scope.data.items[0].selectedBusinessType.id" ng-options="businessType.name for businessType in item.businessTypes | filter:doFilter(item.selectedBusinessType)">
            </select>

在控制器中:

  $scope.doFilter  = function(id) {
    console.log("selectedId:" + Object.values(id))
    return function(value, index, array) {
      console.log(value.id)
      if(value.id == Object.values(id))
        return true;
    }
  };

https://stackoverflow.com/a/39635805/4952634

中的答案有关

答案 1 :(得分:0)

在angularJs版本1.5中,工作选项如下所示: Plunk - Use angularJs 1.5x

  <select class="form-control" ng-model="data.items[$index].selectedBusinessType.id" 
          ng-disabled="item.CanChangeBusinessType" 
          ng-change="changeBusinessType(item, selectedBusinessType.id)">
          <option ng-repeat="businessType in item.businessTypes" ng-value="businessType.id">
            {{businessType.id}}
          </option>
        </select>

但这不适用于1.4+版本。 Plunk - Use angularJs 1.4x

   <div>
        <select class="form-control" ng-model="data.items[$index].selectedBusinessType.id" 
        ng-disabled="item.CanChangeBusinessType" 
        ng-change="changeBusinessType(item, selectedBusinessType.id)">
          <option ng-repeat="businessType in item.businessTypes" ng-value="businessType.id">
            {{businessType.id}}
          </option>
        </select>
      </div>

第二个可行选项-使用ng-option: Ng-Option using

  <select class="form-control" ng-model="item.selectedBusinessType" 
        ng-options="option.id as option.name for option in data.businessTypes"
        ng-disabled="item.CanChangeBusinessType" 
        ng-change="changeBusinessType(item, item.selectedBusinessType, '{{item.selectedBusinessType}}')">
        <option value="">-- Не указан --</option>
      </select>