代码之家  ›  专栏  ›  技术社区  ›  safeena rasak

角度ui-select2未在编辑模式下设置值

  •  1
  • safeena rasak  · 技术社区  · 7 年前
    <select id="e1" style="width:100%" ui-select2  tabindex="-1" ng-init="GetAllAgent()" ng-model="Agent" ng-options="ctr as ctr.AgentName for ctr in ListAgent track by ctr.AgentId" ng-change="GetSubAgentList(Agent.AgentId)">
    <option value=""> </option></select>
    

    当它处于编辑模式时,无法使用以下代码设置默认值

     angular.forEach($scope.ListAgent, function (value, index) {            
                if (value.AgentId == HR.AgentId) {
                    $scope.Agent = $scope.ListAgent[index];
                }
            })
    
     $scope.ListAgent= [{ AgentId: "0", AgentName:"Ann" }, {  AgentId: "1", AgentName:"Muh" }];
    

    HR.AgentId=1
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Maxim Shoustin    7 年前

    首先, ui-select2 ng-options : https://github.com/angular-ui/ui-select2/issues/252

    进一步的 不适用于 ng-repeat https://github.com/angular-ui/ui-select2/issues/162

    解决方案是使用 input :

     <input ui-select2="select2Options" ng-model="Agent"/>
    

    select2Options 是:

    $scope.select2Options = {
      data:$scope.ListAgent
    }
    

    列表应具有以下结构: [{id,text}] 所以 $scope.ListAgent

     $scope.ListAgent= [{ id:0, text:"Ann" }, { id:1, text:"Muh" }];
    

    Demo Plunker


    对于占位符添加 data-placeholder="----"