admin管理员组文章数量:1355665
View not updating when checkbox is checked. Should I used $scope.$apply?
I need the Current state
to change between YES and NO depending on whether the checkbox is checked or not. Here's the code on JSbin
<!DOCTYPE html>
<html ng-app="notesApp">
<head>
<title>Notes App</title></head>
<script src=".js/1.3.8/angular.js"></script>
<body ng-controller="MainCtrl as ctrl">
<div>
<h2>What are your favorite sports?</h2>
<div ng-repeat="sport in ctrl.sports">
<br/>
<label ng-bind="sport.label"></label>
<div>
<input type="checkbox"
ng-model="sport.selected"
ng-true-value="YES"
ng-false-value="NO">
</div>
<div>
Current state: {{sport.selected}}
</div>
</div>
</div>
</body>
</html>
And here's the JS:
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.sports = [
{label: 'Basketball', selected: 'YES'},
{label: 'Cricket', selected: 'NO'},
{label: 'Soccer', selected: 'NO'},
{label: 'Swimming', selected: 'YES'}
];
}]);
View not updating when checkbox is checked. Should I used $scope.$apply?
I need the Current state
to change between YES and NO depending on whether the checkbox is checked or not. Here's the code on JSbin
<!DOCTYPE html>
<html ng-app="notesApp">
<head>
<title>Notes App</title></head>
<script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.3.8/angular.js"></script>
<body ng-controller="MainCtrl as ctrl">
<div>
<h2>What are your favorite sports?</h2>
<div ng-repeat="sport in ctrl.sports">
<br/>
<label ng-bind="sport.label"></label>
<div>
<input type="checkbox"
ng-model="sport.selected"
ng-true-value="YES"
ng-false-value="NO">
</div>
<div>
Current state: {{sport.selected}}
</div>
</div>
</div>
</body>
</html>
And here's the JS:
angular.module('notesApp', [])
.controller('MainCtrl', [function() {
var self = this;
self.sports = [
{label: 'Basketball', selected: 'YES'},
{label: 'Cricket', selected: 'NO'},
{label: 'Soccer', selected: 'NO'},
{label: 'Swimming', selected: 'YES'}
];
}]);
Share
Improve this question
asked Jan 2, 2015 at 0:59
raneshuraneshu
4132 gold badges18 silver badges48 bronze badges
2 Answers
Reset to default 10You need to add quotations because it's an angularjs expression and it also needs to be a constant. If YES is used for an angular expression normally angular would look for a scope variable named YES, but that's not going to be a constant angular expression as the value of YES could change.
Instead you want to just set the value to 'Yes' as you want to assign the value as the constant string 'YES'. If you looked in the console you would have seen: Expected constant expression for 'ngTrueValue', but saw 'YES'
.
<input type="checkbox"
ng-model="sport.selected"
ng-true-value="'YES'"
ng-false-value="'NO'"/>
http://plnkr.co/edit/ehXObXaXicC1BvKyQtmI?p=preview
Refer constexpr in the Error Reference documentation from angularjs. It says
Some attributes used in conjunction with ngModel (such as ngTrueValue or ngFalseValue) will only accept constant expressions.
本文标签: javascriptangular view not updating when checkbox clickedStack Overflow
版权声明:本文标题:javascript - angular view not updating when checkbox clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743947529a2566680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论