Setting ngTrueValue and ngFalseValue to numbers
You can use ngChecked, If the expression is truthy, then special attribute "checked" will be set on the element
<input type="checkbox" ng-model="checkbox" ng-true-value="1" ng-false-value="0" ng-checked="checkbox == 1" />
And you can use $scope.$watch
to convert it to number
$scope.$watch(function(){ return $scope.checkbox;}, function(){ $scope.checkbox = Number($scope.checkbox); console.log($scope.checkbox, typeof $scope.checkbox);},true);
I have created directive for that, seems to work fine:
angular.module('app').directive('cdTrueValue', [function() { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ngModel) { ngModel.$parsers.push(function(v){ return v ? scope.$eval(attrs.cdTrueValue) : scope.$eval(attrs.cdFalseValue); }); ngModel.$formatters.push(function(value) { return value === scope.$eval(attrs.cdTrueValue); }); } };}]);
Usage:
<input type="checkbox" ng-model="checkbox" cd-true-value="1" cd-false-value="0" />
HTML attributes do not have any types. They can not contain anything else, then a string, so it is always a string. End of story.
You can not differentiate between between 1
and "1"
in an HTML attribute. Angular tries to keep up with that, so only strings will work.