Having trouble with the view not updating when the checkbox is checked. Wondering if I should use $scope.$apply? I want the Current state
to toggle between YES and NO based on whether the checkbox is checked or unchecked. Check out the code on JSbin here.
<!DOCTYPE html>
<html ng-app="notesApp">
<head>
<title>Notes App</title></head>
<script src="https://cdnjs.cloudflare.com/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 JavaScript:
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'}
];
}]);