Thomas Juranek Thomas Juranek - 3 months ago 13
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": "some@email.com",
"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
Answer

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