ng-repeat vs ng-options 哪个最适合我 [英] ng-repeat vs ng-options which is best for me

查看:24
本文介绍了ng-repeat vs ng-options 哪个最适合我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在下拉列表中显示 JSON 数据,为此我有两个选项,一个是使用 ng-repeat,另一个是 ng-options.

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-重复代码:

在 html 文件中:

in html file :

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

并在脚本文件中:

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

和其他一个 ng-options :

and other one ng-options :

在 html 文件中:

in html file :

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

在脚本文件中:

$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 .

推荐答案

我认为 ng-options,因为它旨在用于这种情况.

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

Angularjs 文档:-

ngOptions 为元素提供了迭代器工具,当您希望选择模型绑定到非字符串值时,应使用该工具代替 ngRepeat.这是因为选项元素目前只能绑定到字符串值.

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.

这篇关于ng-repeat vs ng-options 哪个最适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆