ng-repeat 和 ng-options 之间有什么区别,为什么它们的行为方式不同? [英] What are the differences between ng-repeat and ng-options and why do they not behave the same way?
问题描述
ng-options
和 ng-repeat
有何不同?
在下面的代码中,我有一个 ng-repeat
遍历一个人的列表:
In the following code, I have an ng-repeat
that iterates through a list of people:
<select ng-model="selectedPerson" >
<option ng-repeat="obj in people" value="{{obj.id}}">{{obj.name}}</option>
</select>
这是我认为使用 ng-options
时等效的选择框:
Here is what I believe to be an equivalent select box in using ng-options
:
<select ng-model="selectedPerson" ng-options='obj.name for obj in people'></select>
我希望他们的行为相同,但他们没有.为什么?
I would expect them to behave the same, but they do not. Why?
$scope.people = [
{
id: 0,
name: 'Leon',
music: [
'Rock',
'Metal',
'Dubstep',
'Electro'
]
},
推荐答案
ng-repeat 为每次迭代创建一个新的作用域,因此性能不如 ng-options.
ng-repeat creates a new scope for each iteration so will not perform as well as ng-options.
对于小列表,没有关系,但较大的列表应该使用 ng-options.除此之外,它在指定迭代器方面提供了很大的灵活性,并提供了优于 ng-repeat 的性能优势.
For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat.
这篇关于ng-repeat 和 ng-options 之间有什么区别,为什么它们的行为方式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!