【问题标题】:angular ui-select2 not set value in edit mode角度 ui-select2 未在编辑模式下设置值
【发布时间】:2017-09-30 05:23:56
【问题描述】:
<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

【问题讨论】:

  • 请发帖$scope.ListAgentHR.AgentId 或在Plunker 中发布演示
  • Questn 已编辑。如果有任何 anthr 方法来设置 dropdwn 值请分享

标签: angularjs ui-select ui-select2 angular-ui-select


【解决方案1】:

首先,ui-select2 不支持ng-optionshttps://github.com/angular-ui/ui-select2/issues/252

此外,ui-select2 不适用于 ng-repeathttps://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="----"

【讨论】:

  • 但是这里ng-options 支持ui-select2 并且下拉菜单正在工作。使用$scope.Agent.AgentId 获取AgentId ..那么为什么它不能正常工作select
  • 在一个页面中使用多个选择时使用您的解决方案$scope.select2Options = { data:$scope.ListAgent }
  • @safeenarasak $scope.select2Options ... $scope.select2OptionsTwo ....
  • 如果我使用 `$scope.GetAllAgent = function () { $http({ method: "get", url: "/api/Agent/GetAllAgent" } ).then(function (response) { $scope.ListAgent = response.data; }) };` 下拉菜单无法绑定
猜你喜欢
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 2011-09-25
  • 2018-11-17
  • 1970-01-01
相关资源
最近更新 更多