Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse checkbox value in angular2

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; });
    }
  };
})
like image 544
kplates Avatar asked May 19 '26 05:05

kplates


1 Answers

why you need directive for this simple thing

you can achieve like this

<input type="checkbox" [checked]="!value" (change)="value= !value" />
like image 107
CharanRoot Avatar answered May 20 '26 18:05

CharanRoot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!