0

I have 2 checkboxes - Yes & No and I have a single model property - IsChecked to handle it which is nullable C#. When I try to bind this property and use ! operator during ngmodel it fails and throws exception. I would like to mark Yes checked when IsChecked true and No when IsChecked false. The reason I have nullable property because it is optional and user have option to select either of the checkbox or leave it as it is.I have seen some discussion related to this and usage of elvis operator but couldn't find a solution.

FYI I am using AnuglarJS 1.4

Please let me know how can this we handle in angular binding apart from using old JS style through setting using ID.

Sample code -

input type="checkbox" id="chkIsYes" ng-model="vm.IsChecked"
input type="checkbox" id="chkIsNo" ng-model="!vm.IsChecked"

Error throws when I use !vm.IsChecked

Punit
  • 1,347
  • 3
  • 20
  • 39
  • So is this [tag:angularjs] **or** [tag:angular]? You need to include the component code and html template, consider creating an [mcve] using https://stackblitz.com. I am also not sure how c# fits in with your question as it should have nothing to do with the angular/angularjs code at all. – Igor Jan 24 '19 at 16:04
  • @Igor - I was using c# model so wanted to add that as a tag. Sure wiill inculde this. It is angular js. – Punit Jan 24 '19 at 16:06
  • That is not proper binding syntax, binding is an assignment to a (models) field. You have an expression, hence the error. ["The ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope..."](https://docs.angularjs.org/api/ng/directive/ngModel) – Igor Jan 24 '19 at 16:11
  • Once the data has been serialized into JSON, the code that data originated from ceases to be relevant unless changes to that code are part of the question. That isn't the case here. The c# tag should be removed, and HTML or JavaScript added. –  Jan 24 '19 at 16:11
  • @Amy - Thanks, I have corrected it. – Punit Jan 24 '19 at 16:20
  • Possible duplicate of [Error: ngModel:nonassign Expression is non-assignable](https://stackoverflow.com/q/24177707/1260204) – Igor Jan 24 '19 at 16:32

0 Answers0