【问题标题】:Assigning value to checkboxes through ng-model通过 ng-model 为复选框赋值
【发布时间】:2017-06-28 14:29:17
【问题描述】:

我有一个复选框,我希望其值为 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。我该怎么做?

【问题讨论】:

标签: javascript angularjs checkbox angular-ngmodel


【解决方案1】:

使用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">

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

app.controller('MainCtrl', function($scope) {
  
});
<!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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多