Thomas Juranek Thomas Juranek - 5 months ago 24
AngularJS Question

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

I have an array like this:

var data = [{
"chamberName": "Community App",
"representativeEmail": "",
"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!

atn atn

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" .. />

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

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