ng-repeat vs ng-options which is best for me

Shekkar picture Shekkar · Jun 30, 2014 · Viewed 13.8k times · Source

i have to display the JSON data in drop down list ,for that i have two options one of the options is By using ng-repeat and other one is ng-options.

ng-repeat code :

in html file :

<select>
<option ng-repeat="prod in testAccounts" value="{{prod.id}}">{{prod.name}}</option>
</select>

and in script file:

$http.get('document.json').success(function (data) 
{
    $scope.testAccounts = angular.fromJson(data);
 }

and other one ng-options :

in html file :

<select ng-model="selectedTestAccount" ng-options="c as c.name for c in testAccounts1"></select>

in script file:

$http.get('document.json').success(function (data) 
{
    $scope.testAccounts1 = data;
    $scope.selectedTestAccount = $scope.testAccounts1[0];
}

Now i want to know which one is best for my project to improve the performance .Any guidelines please .

Answer

Mritunjay picture Mritunjay · Jun 30, 2014

I think that ng-options, because that is meant to be used in cases like this.

Angularjs Docs:-

ngOptions provides an iterator facility for the element which should be used instead of ngRepeat when you want the select model to be bound to a non-string value. This is because an option element can only be bound to string values at present.