Here's my code:
<input type="checkbox" ng-model="variable">
In this case variable
will be true
or false
according the status of checkbox. I need to set not true
/false
but "+"
/"-"
instead. What is the simplest way to do this?
Thanks.
According to the docs, you should use ng-true-value
and/or ng-false-value
to override true
and false
values:
<input type="checkbox" ng-model="variable" ng-true-value="+" ng-false-value="-">
Use ng-click to do that.
<input type="checkbox" ng-model="variable" ng-click="check()">
In the controller :
$scope.check = function() {
$scope.selected = $scope.variable === true ? '+' : '-';
};
Live Plunker.
Just setup a watch on your checkbox model.
HTML:
<input type="checkbox" ng-model="isChecked">
JavaScript:
// set initial value
$scope.isChecked = $scope.variable === '+';
// watch for changes to the checkbox model
$scope.$watch('isChecked', function(newVal) {
$scope.variable = newVal ? '+' : '-';
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With