通过ng-model为复选框赋值

时间:2017-06-28 06:24:29

标签: javascript angularjs checkbox angular-ngmodel

我有一个复选框,我想要一个值= 0。我还给它分配了一个ng-moel

<input type="checkbox" class="input-group inline form-control" value="0" name="" id="deletelanguage" ng-show="IsVisible" ng-model="delStat">

我的控制器中的范围看起来像这样。

$scope.delStat="";

如果选中复选框,我想将其更改为1。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

使用ng-true-value ng-false-value,如下所示

<input type="checkbox" class="input-group inline form-control" ng-true-value='1'  id="deletelanguage" ng-show="IsVisible" ng-model="delStat">

&#13;
&#13;
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  
});
&#13;
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
   <input type="checkbox" name="mycheck" ng-true-value='1'  ng-false-value='0'  ng-model="val">Check</input>
   <br>Seleted Value :{{val}}
  </body>

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