[英]How to set default value checked in a checkbox from database using AngularJS
While editing the record the data comes in their respective fields for editing, but the checkbox is not checked if its value is 1
. 编辑记录时,数据会在各自的字段中进行编辑,但如果其值为1
则不会选中该复选框。
It should be in checked state if it has 1
and not checked if it has 0
as the datatype of the field is tinyint
. 它应该处于检查状态,如果它有1
并且没有检查它是否为0
因为该字段的数据类型是tinyint
。 I am retrieving the data from JSON array via PHP. 我正在通过PHP从JSON数组中检索数据。 I am trying to get value by ng-checked="{{rec.isSpecial}}"
but it is not working. 我试图通过ng-checked="{{rec.isSpecial}}"
获得价值,但它无效。
Here is my HTML: 这是我的HTML:
<div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <div class="checkbox c-checkbox"> <label> <input type="checkbox" ng-checked="{{rec.isSpecial}}" ng-model="rec.isSpecial"> <span class="fa fa-check"></span>Special </label> </div> </div> </div>
here is my JS 这是我的JS
$scope.rec = $resource('api/AdminArea/category/edit/:id', {
id: id
}).get(function(data) {
$scope.rec = data;
});
Since your data returns numbers and not true or false values you could do like so: 由于您的数据返回数字而不是true或false值,您可以这样做:
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox c-checkbox">
<label>
<input type="checkbox" ng-checked="rec.isSpecial==1" ng-model="rec.isSpecial">
<span class="fa fa-check"></span>Special
</label>
</div>
</div>
</div>
You don't need ng-checked
at all, ng-model
is enough for it to work. 您根本不需要进行ng-checked
, ng-model
足以使其工作。 If your rec.isSpecial
is set to 1
for it to be checked then just your input
like this: 如果您的rec.isSpecial
设置为1
以便进行检查,那么只需input
如下:
<input type="checkbox" ng-model="rec.isSpecial" ng-value="1" />
and the Angular will automatically check that checkbox. 并且Angular会自动检查该复选框。
The expression ng-checked={{ ... }}
expects a true
or false
statement. 表达式ng-checked={{ ... }}
需要一个true
或false
语句。 If you would set rec.isSpecial = true
, in your Angular controller, your code should work! 如果你设置rec.isSpecial = true
,在你的Angular控制器中,你的代码应该可行!
'use strict'; var app = angular.module('MyApp',[]); app.controller('myController', function ($scope) { $scope.isSpecial =true; })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div class="form-group" ng-app ="MyApp" ng-controller="myController"> <div class="col-lg-offset-2 col-lg-10"> <div class="checkbox c-checkbox"> <label> <input type="checkbox" ng-checked="{{isSpecial}}" ng-model="isSpecial"> <span class="fa fa-check"></span>Special </label> </div> </div> </div>
You don't really need the directive ng-checked
if you are using ng-model
in the tag input. 如果在标签输入中使用ng-model
,则实际上不需要指令ng-checked
。
Since it is a checkbox, the model will be a boolean, so you need to make a cast. 由于它是一个复选框,因此模型将是一个布尔值,因此您需要进行转换。 This example will work 这个例子可行
HTML HTML
<div data-ng-app="app">
<div data-ng-controller="MainController">
Check
<input type="checkbox" data-ng-model="isSpecial">
</div>
</div>
JS JS
angular.module('app', []);
angular.module('app')
.controller('MainController', mainController);
mainController.$inject = ['$scope'];
function mainController($scope){
$scope.check = 0;
$scope.isSpecial = ($scope.check === 1) ? true : false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.