NG-重复VS NG选项是最适合我 [英] ng-repeat vs ng-options which is best for me

查看:130
本文介绍了NG-重复VS NG选项是最适合我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下拉列表中显示JSON数据,对于我有两个选择的方案之一是采用NG-重复和另一种是NG选项。

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-重复code:

ng-repeat code :

在HTML文件:

<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选项:

在HTML文件:

<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-选项,因为这意味着在这样的情况下被使用。

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

Angularjs文档: -

ngOptions提供用于当你想选择的模型被结合到非字符串值应该被用来代替ngRepeat的元素的迭代设施。这是因为一个选项元件只能在present绑定到字符串值

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-重复VS NG选项是最适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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