簡體   English   中英

禁用AngularJS形式的select標記內的選項

[英]Disable an option inside a select tag in AngularJS form

我有一個表格,其中有兩個選擇。 我想要實現的是,如果在第一個選擇中選擇了“任何”選項,請在另一個選擇中禁用其中一個選項。

因此,在這里,如果選擇了“ 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>

預先感謝您提供的任何幫助。

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

<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/

您為什么不嘗試選擇內部的選項標簽? 例如:

<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">
    <option value="{{option.type + ' ' + option.command}}" ng-disabled="option.type == 'Combination ban' && vm.rule.scriptType == 'Any'">{{option.type + ' ' + option.command}}</option>
</select>

請讓我知道它是否無效。 我沒有測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM