Sreehari S Sreehari S - 12 days ago 5
AngularJS Question

AngularJS : Display a property value of multiple objects in an input text field, comma separated

I have an array of JSON objects in the format

users=[{id:'1',name:'ABC'},{id:'2',name:'DEF'},............]


I need to display the names of all users in an
input text field
comma separated as below image.

enter image description here

I tried using
ng-list
directive, but for that I had to first loop through the user objects and store all the names in a separate array and use that array as
ng-model
for the
<input>
element. Is there an easy, alternate way in angularjs?

Answer

A simple JS solution Use Map function to get the desired property in array then use a join over it

 var users=[{id:'1',name:'ABC'},{id:'2',name:'DEF'}];

 $scope.userModel = users.map(function(el){return el.name}).join(",");

 <input type="text" ng-model="userModel">
Comments