Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significance of ng-repeat-start vs ng-repeat

Tags:

angularjs

I am trying to understand the significance of ng-repeat-start over ng-repeat. The angular documentation provides the following example for ng-repeat-start

<header ng-repeat-start="item in items">
  Header {{ item }}
</header>
<div class="body">
  Body {{ item }}
</div>
<footer ng-repeat-end>
  Footer {{ item }}
</footer>

But the same can be achived using ng-repeat,

<div ng-repeat="item in items">
  <header>
    Header {{ item }}
  </header>
  <div class="body">
    Body {{ item }}
  </div>
  <footer>
    Footer {{ item }}
  </footer>
</div>

Can someone explain the significance of ng-repeat-start.? Thanks.

like image 529
Muthukumar Avatar asked Mar 09 '15 10:03

Muthukumar


People also ask

Why do we use NG-repeat?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object.

What is the difference between ng-repeat and ngFor?

ng-repeat created inherited child scope for each element of collection, while *ngFor creates local variable in the that block.

Does ng-repeat create a new scope?

Directives that Create Scopes In most cases, directives and scopes interact but do not create new instances of scope. However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.

What is data ng-repeat?

The data-ng-repeat allows the HTML to be validated through validators that do not understand Angular. The documentation is here with directives. This is from the docs: Angular normalizes an element's tag and attribute name to determine which elements match which directives.

What is the use of ng-repeat and Ng-for in angular?

Both ng-repeat and ng-for are using to iterate a loop over an array. ng-repeat is used in Angular 1 version. What is the use of ng-content in Angular? So generally most of the people write the above code like this: What is NgModel in Angular? The ngmodel directive binds the value of HTML controls (input, select, textarea)

What is the difference between ng-repeat and vs-repeat for $digest?

On the left size the vs-repeat directive was used, whereas on the right side there is a plain regular ng-repeat. Above each of those lists the $digest cycle duration gauge is placed (updates every second) so you can see how many milliseconds it takes to run a $digest with and without vs-repeat.

What is ng-repeat in HTML?

1 Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. 2 Syntax. Supported by all HTML elements. 3 Parameter Values. An expression that specifies how to loop the collection. 4 More Examples

What is the use of ngmodel in angular?

The ngModel directive is used for two-way data binding. This directive is commonly used to modify the behavior of an existing element i.e usually the <input> element. ngModel sets the <input> element’s display value property and it will also re What is the difference between Angular 6 and Angular 9?


3 Answers

I thought I'd add my answer, as no one touched on a very important reason for having these directives available. ng-repeat will not work correctly in certain scenarios when using html tables. Using ng-repeat-start is the only way to accomplish certain things.

Imagine you want to display your data like this using html tables:

Grouped Data Mockup

And this is your data set:

groups = [
    {
        name: "Group 1",
        customers: [
            {id: 123, name: "Foo Inc.", state: "NJ"},
            {id: 234, name: "Bar Co.", state: "AZ"}
        ]
    },
    {
        name: "Group 2",
        customers: [
            {id: 345, name: "Baz LLC", state: "CA"}
        ]
    }
];

Using ng-repeat-start and ng-repeat-end you can do this:

<table>
    <tr>
        <th>ID</th>
        <th>Customer</th>
        <th>State</th>
    </tr>
    <tr ng-repeat-start="group in groups">
        <td colspan="3" style="text-align:center">{{group.name}}<td>
    </tr>
    <tr ng-repeat-end ng-repeat="customer in group.customers">
        <td>{{customer.id}}</td>
        <td>{{customer.name}}</td>
        <td>{{customer.state}}</td>
    </tr>
</table>

Notice the ng-repeat-end followed by another regular ng-repeat. This ends the matching ng-repeat-start but initiates another repeat on the customers array, since we are still in the original ng-repeat-start scope when calling ng-repeat-end, we still have access to the group object.

Keep in mind, this is a very trivial example, but as the table structure becomes more complicated, the only way to accomplish things like this is to use ng-repeat-start and ng-repeat-end

like image 163
jtate Avatar answered Oct 10 '22 10:10

jtate


The significance of these two directives is similar: they repeat HTML-tags. The difference is only that with help of ng-repeat-start you could repeat few tags starting from tag with ng-repeat-start and finishing by ng-repeat-end.

For example you have next code:

<div>
  Item # {{item}}
</div>
<div>Whether you repeat me?</div>

So now we can add 2 directives for these code.
With ng-repeat:

<div ng-repeat="item in items">
  Item # {{item}}
</div>
<div>
  This code will not be repeated
</div>

With ng-repeat-start and ng-repeat-end:

<div ng-repeat-start="item in items">
  Item # {{item}}
</div>
<div ng-repeat-end="">This code will be repeated</div>

So now you can see that in the first case just div with ng-repeat directive repeats, but in the second case both your divs will be repeated.

You can see Demo and play with it:

Demo: http://plnkr.co/edit/R778lWTABVF3Hy16CAca

like image 19
ababashka Avatar answered Oct 10 '22 09:10

ababashka


The ng-repeat-start directive works the same as ng-repeat, but will repeat all the HTML code (including the tag it’s defined on) up to and including the ending HTML tag where ng-repeat-end is placed

like image 2
Ramesh Rajendran Avatar answered Oct 10 '22 11:10

Ramesh Rajendran