lyzz
lyzz

Reputation: 128

Angular generating multiple ng-repeats

In my angular application (angular 1.4.9) I have a problem, where multiple ng-repeats are generated when I do this:

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'];
  }
}

But the generated HTML in the dom is: enter image description here

Why does angular repeat the ng-repeats?

Upvotes: 0

Views: 94

Answers (3)

Can Guney Aksakalli
Can Guney Aksakalli

Reputation: 1330

If you don't want to repeat parent element, here is html:

<div >
  <span ng-repeat="suggestion in startLocationSuggestions">{{suggestion}}</span>
</div>

Output will be as:

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

This is how ngRepeat works.

Upvotes: 1

not-found.404
not-found.404

Reputation: 70

Because it just takes your template as is (div ng-repeate lalala), clones it N times, fills with data and put into DOM.

Upvotes: 1

GHB
GHB

Reputation: 947

this is basically how the ng-repeat works! at the start it writes a comment like:

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

then for every element that's been created with ng-repeat, after creating it (WITH the ng-repeat attribite inside), writes another comment:

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

Upvotes: 1

Related Questions