在我的角度应用程序(角度1.4.9)中,我遇到了一个问题,当我这样做时会生成多个ng-repeats:
HTML:
<div class="form-group col-md-3">
<input ng-model="main.startLocation" ng-change="main.getAddressSuggestions(main.startLocation, 'startLocation')" type="text" class="form-control input-lg" placeholder="fx. Aarhus" tabindex=1 name="country" [![enter image description here][1]][1] />
<label>or select a location from list:</label>
<div ng-repeat="suggestion in main.startLocationSuggestions">
<span>{{suggestion}}</span>
</div>
</div>
controller.js
getAddressSuggestions(address, inputName) {
if (inputName === "startLocation") {
/* var tmpArray = data;
for(var item of tmpArray)
this.startLocationSuggestions = data;*/
this.startLocationSuggestions = ['dada', 'daa'];
}
}
为什么角重复ng重复?
答案 0 :(得分:1)
这基本上是ng-repeat的工作原理! 在一开始它写了一个评论,如:
<!-- ngRepeat: x in items -->
然后对于使用ng-repeat创建的每个元素,在创建它之后(使用ng-repeat attribite),写下另一条评论:
<!-- end ngRepeat: x in items -->
答案 1 :(得分:1)
因为它只需要你的模板(div ng-repeate lalala),克隆它N次,填充数据并放入DOM。
答案 2 :(得分:1)
如果你不想重复父元素,这里是html:
<div >
<span ng-repeat="suggestion in startLocationSuggestions">{{suggestion}}</span>
</div>
输出将为:
<div>
<!-- ngRepeat: suggestion in startLocationSuggestions -->
<span ng-repeat="suggestion in startLocationSuggestions" class="ng-scope ng-binding">dada</span>
<span ng-repeat="suggestion in startLocationSuggestions" class="ng-scope ng-binding">daa</span>
</div>
这就是ngRepeat的工作方式。