javascript - 通过 ng-model 为复选框赋值

标签 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">

我的 Controller 中的范围如下所示。

$scope.delStat="";

如果选中该复选框,我想将其值更改为 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>

关于javascript - 通过 ng-model 为复选框赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795039/

相关文章:

javascript - 如何删除正则表达式中特殊字母后的括号

javascript - JQuery 4按钮菜单,点击隐藏div并显示其他div,反之亦然

javascript - 检查表格单元格中存在什么类型的元素

jquery - 删除未选中复选框的值

javascript - jQuery 元素添加事件不响应某些语法

javascript - $location.Path 不在 iphone 上运行

javascript - AngularJS - 如何使用 "require"在指令中获取指令 Controller

AngularJS - 设置在指令模板中定义的模型

java - 将复选框值保存到 sqlite 数据库

javascript - 将复选框输入值放入复选框本身