Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a scope inside a ng-repeat just for the last element?

I want to call a scope function inside my ng-repeat just for the last element, like this:

<div ng-repeat="patient in patients" >
  <div ng-if="$last" ng-load="myFunction()"></div>
</div>

But ng-load doesn't work!

like image 997
R3tep Avatar asked Mar 03 '14 16:03

R3tep


People also ask

Where is the last element in NG-repeat?

You can use $last variable within ng-repeat directive. Take a look at doc. Where computeCssClass is function of controller which takes sole argument and returns 'last' or null .

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.

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What is difference between data ng-repeat and 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.


2 Answers

I think this should work:

  <div ng-repeat="element in array" ng-init="$last && myFunction(element)">
       {{element}}          
  </div>

There you have working JSFiddle.

like image 77
PrimosK Avatar answered Oct 06 '22 23:10

PrimosK


Use ng-init="$last && myFunction()", that should do the same thing.

like image 41
Zack Argyle Avatar answered Oct 06 '22 22:10

Zack Argyle