我有一个复选框,我希望其值为 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/