YoroDiallo YoroDiallo - 1 year ago 66
AngularJS Question

'or', 'and' in ng-model angularjs

I have a list of items, some of them has property value, some has property default. And I need to bind it into the input[text] with ng-repeat.

Array is looks like:

$scope.arr = [

And in html:

<div ng-repeat='item in arr'>
<input type='text' ng-model='item.value || item.default'>

It works, but I have error message in console "[ngModel:nonassign] http://errors.angularjs.org/1.5.8/ngModel/nonassign?p0=item.value%20%7C%7CNaNtem.default&p1=%3Cinput%20type%3D%22text%22%20ng-model%3D%item.value%20%7C%7C%item.default%22%20class%3D%22ng-pristine%20ng-untouched%20ng-valid%22%3E". Because ng-model doesn't work with expression..
Perhaps there is another way to solve it?

Plnkr example

Answer Source

You can do it this way:

<div ng-repeat='item in arr'>
   <input type='text' ng-hide='item.default' ng-model='item.value'>
   <input type='text' ng-show='item.default' ng-model='item.default'>

Plunker example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download