Ajit Hingmire
Ajit Hingmire

Reputation: 221

created only one input for multiple values in ng-repeat

I am looping through JSON for user id's & want to assign all the user id from json to one html field. However ng-repeat creates a new input tag for each value. Here is the fiddle. http://fiddle.jshell.net/7Ly6q14c/ Is there a way I can loop through all userIds & assign them in one input element?

<div ng-app="" ng-init="names=[
  {
    'userId': '15',
    'mgId': '1',
    'mgName': 'Oracle'
  },
  {
    'userId': '16',
    'mgId': '2',
    'mgName': 'SunCorp'
  }
]">
<p>Looping with objects:</p>
  <div ng-repeat="x in names">
  <input ng-model="x.userId"></input></div>
</div>

Upvotes: 0

Views: 1099

Answers (1)

Sabarish
Sabarish

Reputation: 531

Please try the below code

Controller:

$scope.names = [
    {
      'userId': '15',
      'mgId': '1',
      'mgName': 'Oracle'
    },
    {
      'userId': '16',
      'mgId': '2',
      'mgName': 'SunCorp'
    }
  ]

  $scope.allIds = [];

  angular.forEach($scope.names, function(name) {
    $scope.allIds.push(name.userId);
  });

HTML:

<input ng-model="allIds"></input>

Also you can check in this fiddle

Upvotes: 1

Related Questions