Thomas Juranek
Thomas Juranek

Reputation: 343

how to use Angular ng-change on input to change json data

I have an array like this:

var data = [{
    "chamberName": "Community App",
    "representativeEmail": "[email protected]",
    "primaryColor": "#325490",
}]

And an input on a form like this:

<input type="text" class="form-control" id="chamberName" ng-change="" placeholder="Chamber Name">

I'd like to use ng-change to change the array value that matches the changing input value. I can't figure out how to get the input value though. I am thinking something like:

ng-change="data[0].chamberName = inputValue";

Any help would be great!

SIDE NOTE: You need to use $parent when using ng-include.

Upvotes: 0

Views: 520

Answers (1)

atn
atn

Reputation: 904

You need to use ngModel.

Why your original data in array?

If you need to edit only one instance it is simpler to use object and pass object's property to ngModel

<input .. data-ng-model="data.chamberName" .. />

Or if you are going to edit pool of objects, than you can surround it with ngRepeat

<fieldset data-ng-repeat="elm in data">
  <input .. data-ng-model="elm.chamberName" .. />
  ..
</fieldset>

Or if you still want to store your single object in array

<input .. data-ng-model="data[0].chamberName" .. />

Upvotes: 1

Related Questions