带有单选按钮的Angular checked属性

时间:2017-07-04 13:27:26

标签: javascript angularjs radio-button

这可能是一个简单的问题我试图将checked属性设置为基于变量值的单选按钮

$scope.art.is_copyright_protected = ($scope.art.is_copyright_protected==1)?true:false;

 <div class="form-inline checkbox-wrap">
      <fieldset class="form-group">
        <input name="is_copyright_protected"  ng-model="art.is_copyright_protected"     type="radio" id="radio11" >
        <label for="radio11">Yes</label>
     </fieldset>
  <fieldset class="form-group">
      <input name="is_copyright_protected"  ng-model="art.is_copyright_protected"   type="radio" id="radio21"><label for="radio21">No</label>
</fieldset>
</div>

但问题是从不检查单选按钮

1 个答案:

答案 0 :(得分:0)

您应该使用ng-value指令

  

将给定表达式绑定到元素的值。

<input ng-value="1" ng-model="art.is_copyright_protected" type="radio">

(function(angular) {
  'use strict';
  angular.module('myApp', [])
    .controller('Controller', ['$scope', function($scope) {
      $scope.art = {
        is_copyright_protected: 0
      };
    }]);
})(window.angular);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="Controller">
    <div class="form-inline checkbox-wrap">
      <fieldset class="form-group">
        <input name="is_copyright_protected" ng-value="1" ng-model="art.is_copyright_protected" type="radio" id="radio11">
        <label for="radio11">Yes</label>
      </fieldset>
      <fieldset class="form-group">
        <input name="is_copyright_protected" ng-value="0" ng-model="art.is_copyright_protected" type="radio" id="radio21"><label for="radio21">No</label>
      </fieldset>

      {{art.is_copyright_protected}}
    </div>
  </div>
</div>