单击时切换单选按钮 angularjs

Toggle Radio buttons on click angularjs

如何在单击每个单选按钮时切换单选按钮应该启用相应的元素 say 元素,反之亦然。 我试过的代码:

复选框 1:<input type="radio" name="somethign" value="1" ng-model="checkboxSelection" ng-click="disable1='true';disable2='false'"/>

<select ng-model="something" ng-options="r.id as r.name for r in data1" ng-disabled="disable1">

复选框 2:<input type="radio" name="somethign" value="2" ng-model="checkboxSelection" ng-click="disable2='true';disable1='false';"/>

<select ng-model="something" ng-options="r.id as r.name for r in data2" ng-disabled="disable2">

我不会根据以上内容进行切换code.Can任何人都可以提出不同的选择吗?谢谢

更简洁的实施方式是根据 selected 值禁用 select 项:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-init="checkboxSelection = 1">
  <input type="radio" name="somethign" value="1" ng-model="checkboxSelection"/>First
  <input type="radio" name="somethign" value="2" ng-model="checkboxSelection"/>Second
  <br />
  <select ng-model="something1" ng-disabled="checkboxSelection != 1">
      <option>A</option>
      <option>B</option>
  </select>
  <select ng-model="something2" ng-disabled="checkboxSelection != 2">
    <option>A</option>
    <option>B</option>
  </select>
</div>

就像 Tom 已经提到的(这是首选方式):您可以只读取复选框的模型值并在禁用属性中使用它:

<select ng-model="something" ng-options="r.id as r.name for r in data1"    ng-disabled="checkboxSelection == 1">

当变量设置为布尔值时,您的解决方案(使用禁用变量)也应该有效:

ng-click="disable1=true;disable2=false"

或者,反过来,调整您的 ng-disabled 以显式检查字符串值,如下所示:

ng-disabled="disable1==='true'"