2016-01-23 64 views
0

以我的角應用(角1.4.9)我有一個問題,其中,生成多個NG-重複我這樣做時:角生成多個納克重複序列

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> 

控制器.js文件

getAddressSuggestions(address, inputName) { 
    if (inputName === "startLocation") { 

    /*     var tmpArray = data; 
    for(var item of tmpArray) 
    this.startLocationSuggestions = data;*/ 
    this.startLocationSuggestions = ['dada', 'daa']; 
    } 
} 

但DOM中生成的HTML是: enter image description here

爲什麼角度重複ng重複?

回答

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作品。

1

這基本上是如何ng重複工作! 在一開始就這樣寫了評論:

<!-- ngRepeat: x in items --> 

那麼對於一個已經有NG-重複創建的每個元素,創造它(裏面的NG-重複attribite)後,把另一個註釋:

<!-- end ngRepeat: x in items --> 
1

因爲它只需要你的模板(div-repeate lalala),克隆它N次,填充數據並放入DOM。