在md-radio-button中保存布尔值?

时间:2017-03-29 09:00:59

标签: angularjs radio-button angular-material

我想将true保存在回答上,将false保存在

            <div layout-gt-sm="row">
                Insured?

                <md-radio-group ng-model="vehicle.insured">

                    <md-radio-button value="true" class="md-primary">Yes</md-radio-button>
                    <md-radio-button value="false"> No </md-radio-button>

                </md-radio-group>

            </div>

它将值保存在数据库中,但它不绑定现有值,因此当用户检索记录时没有选择的答案

1 个答案:

答案 0 :(得分:9)

猜猜你的意思是初始化md-radio-group,尝试将value替换为ng-value

&#13;
&#13;
var app = angular.module('angularMaterialApp', ['ngMaterial']);
app.controller('mainCtrl', ['$scope',
  function($scope) {
    $scope.vehicle = {
      Insured: true
    };

  }
]);
&#13;
<html lang="en" ng-app="angularMaterialApp">

<head>
  <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/angular_material/0.8.3/angular-material.min.css">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular-animate.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular-aria.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angular_material/0.8.3/angular-material.min.js"></script>
  <script src='app.js'></script>
</head>

<body ng-controller="mainCtrl">

  <p>Selected Value: <span class="radioValue">{{ vehicle.Insured }}</span>
  </p>
  <md-radio-group ng-model="vehicle.Insured">
    <md-radio-button ng-value="true">Yes</md-radio-button>
    <md-radio-button ng-value="false">No</md-radio-button>
  </md-radio-group>
  <hr />

</body>

</html>
&#13;
&#13;
&#13;