已选中Angular set disabled选项

时间:2016-02-03 12:34:32

标签: javascript html angularjs

拥有下一个选择器:

<select
      ng-model="vm.selectedUserState"
      ng-options="u as u.Group disable when u.Obsolete for u in vm.UserStates">
</select>

当加载页面并禁用当前状态代码(Obsolete为true)时,我看到空选择,我如何解决这个问题(即使禁用某个项目也预先选择一个合适的选项)

2 个答案:

答案 0 :(得分:1)

您的ng-model可能在加载时未定义,如果您希望在控制器中设置初始选定项目ng-model

vm.selectedUserState = vm.UserStates[selectionKeyName]

<强>更新 看看Angular来源我认为不可能:

if (option && !option.disabled) { //Skips if it's disabled <----
  if (selectElement[0].value !== option.selectValue) {
    removeUnknownOption();
    removeEmptyOption();

    selectElement[0].value = option.selectValue;
    option.element.selected = true;
    option.element.setAttribute('selected', 'selected');
  }
} else {
  if (value === null || providedEmptyOption) {
    removeUnknownOption();
    renderEmptyOption();
  } else {
    removeEmptyOption();
    renderUnknownOption();
  }
}

答案 1 :(得分:0)

解决了添加额外检查的问题

<select
      ng-model="vm.selectedUserState"
      ng-options="u as u.Group disable when (u.Obsolete && vm.selectedUserState.Code !== u.Code) for u in vm.UserStates">
</select>