Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-repeat alias in the repeat expression?

Given a template like this:

<li ng-repeat="user in users">
  {{user.firstName}}
</li>

Is there any way to "alias" the "user" so I can do this instead:

<li ng-repeat="SOME MAGIC HERE MAYBE?">
  {{firstName}}
</li>

So that I don't need to type the "user." prefix for each expression?

like image 806
JohnC Avatar asked Feb 06 '14 23:02

JohnC


People also ask

What is Ng-repeat in Angular?

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. Note: Each instance of the repetition is given its own scope, which consist of the current item.

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.

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 $Index in AngularJS?

$index is a way to show which iteration of a loop you're in. If we set up an ng-repeat to repeat over the letters in 'somewords', like so: <div ng-app="app"> <div ng-repeat="item in 'somewords'.split('')"> {{$index + 1}}. {{item}} </div> </div>


1 Answers

Here is the script to put into controller

$scope.scopify = function(scope, obj){
    for (var a in obj) {
        scope[a] = obj[a];
    }        
}

Then call it like this

<li ng-repeat="user in users" ng-init="scopify(this,user)">  
  {{firstName}}
</li>
like image 198
Nihat Avatar answered Oct 16 '22 07:10

Nihat