首页 > 解决方案 > 禁用 AngularJS 表单中选择标记内的选项

问题描述

我有一个表格,有两个选择。我想实现,如果在第一个选择中选择了“任何”选项,则禁用另一个选择中的一个选项。

因此,在这里,如果选择了“Any”(vm.scriptTypes 的一部分,vm 是控制器):

<select name="scriptType" id="scriptType" class="form-control" required
        ng-model="vm.rule.scriptType"
        ng-options="option.name as option.name for option in vm.scriptTypes">
</select>

从 vm.actions (ng-options) 中禁用“组合禁令”(“组合禁令”是 vm.actions 的一部分,它是一个对象,具有“类型”和“命令”键)

<select name="action" id="action" class="form-control" required
        ng-model="vm.rule.action"
        ng-change="vm.rule.action "
        ng-options="(option.type + ' ' + option.command) as (option.type + ' ' + option.command) for option in vm.actions">
</select>

提前感谢您提供的任何帮助。

标签: javascripthtmlangularjsfrontend

解决方案


您可以disable when在 ngOptions https://docs.angularjs.org/api/ng/directive/ngOptions中使用该参数

<div ng-app="TestApp" ng-controller="TestController">
  <select name="scriptType" id="scriptType" class="form-control" required
          ng-model="rule.scriptType"
          ng-options="option.name as option.name for option in scriptTypes">
  </select>
  <select name="action" id="action" class="form-control" required
        ng-model="rule.action"
        ng-change="rule.action "
        ng-options="(option.type + ' ' + option.command) as (option.type + ' ' + option.command) disable when (rule.scriptType == 'Any' && option.type == 'Combination ban') for option in actions">
</select>
</div>

js:

var app = angular.module('TestApp', []);

app.controller('TestController', function($scope) {
  $scope.scriptTypes = [{
    name: 'Any'
  },
  {
    name: 'ron'
  },
  {
    name: 'ron2'    
  }];
  $scope.actions = [{
    type: 'type1',
    command: 'cmd1',
  },
  {
    type: 'type2',
    command: 'cmd2',
  },
  {
    type: 'Combination ban',
    command: 'Combination ban',
  }];
});

https://jsfiddle.net/ojzdxpt1/59/


推荐阅读