Having some troubles reversing the checkbox value in Angular 2.
In Angular 1.x we could make a directive like below.
.directive('invertValue', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$parsers.unshift(function(val) { return !val; });
ngModel.$formatters.unshift(function(val) { return !val; });
}
};
})
Having some troubles reversing the checkbox value in Angular 2.
In Angular 1.x we could make a directive like below.
.directive('invertValue', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$parsers.unshift(function(val) { return !val; });
ngModel.$formatters.unshift(function(val) { return !val; });
}
};
})
Share
Improve this question
edited Jun 15, 2017 at 3:41
Vamshi
9,3614 gold badges40 silver badges55 bronze badges
asked Jun 15, 2017 at 2:02
kplateskplates
7301 gold badge7 silver badges17 bronze badges
2
- you mean making it false if its true? – Sajeetharan Commented Jun 15, 2017 at 2:03
- Yeah the value is in my object is true however the checkbox should display as false. Without changing ngModel to false. i was hoping to create this as a directive so its reusable like the above code – kplates Commented Jun 15, 2017 at 2:05
1 Answer
Reset to default 7why you need directive for this simple thing
you can achieve like this
<input type="checkbox" [checked]="!value" (change)="value= !value" />