根据AngularJS中的单选按钮选择启用/禁用按钮

jet*_*com 6 angularjs

我有一个单选按钮列表(使用ng-repeat)中的项目button(最初禁用ng-disabled)继续.选择单选按钮后,我想启用"继续"按钮.

在Angular中这样做的正确方法是什么?

相关JS:

$scope.companies = [{
        "id": 3,
        "name": "Twitter"
    }, {
        "id": 2,
        "name": "Google"
    }, {
        "id": 1,
        "name": "Apple"
    }]
Run Code Online (Sandbox Code Playgroud)

相关HTML:

<table>
  <tr ng-repeat="company in companies">
     <td>
       <input type="radio" ng-model="companyId" name="companyId" value="{{company.id}}" />{{company.name}}
      </td>
  </tr>
</table>

<button ng-disabled="!companyId">Continue</button>
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

谢谢!

JB *_*zet 17

ngRepeat创建一个新范围,该范围是按钮使用范围的子范围.您可以使用它来修复它

<input type="radio" ng-model="$parent.companyId" .../>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/UZkM8/1/

但更好的解决方案是更新已在范围内的对象:

$scope.userChoice = {};

<input type="radio" ng-model="userChoice.companyId" .../>

<button ng-disabled="!userChoice.companyId">Continue</button>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/UZkM8/3/