Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parent scope value into ng-repeat loop in Angular

This should be an extremely simple question, but all of the workarounds I've found are complex. I'm looping through an array of objects in using ng-repeat in a template as follows:

<div class="row-fluid" ng-repeat="message in messages.current|filter:'draft'">      {{ message.subject }} ... {{ campaign.name }} ... </div> 

Since the ng-repeat creates a new scope, the 'campaign' object from the controller doesn't seem to be accessable. Is there any way (aside from adding the campaign object to every item in my array) of getting that value?

Thanks in advance.

like image 328
Wandering Digital Avatar asked May 15 '13 20:05

Wandering Digital


People also ask

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.

Why we use NG-repeat in Angular?

Example# ng-repeat is a built in directive in Angular which lets you iterate an array or an object and gives you the ability to repeat an element once for each item in the collection. To repeat multiple DOM elements by defining a start and an end point you can use the ng-repeat-start and ng-repeat-end directives.


1 Answers

You can access the parent scope by using $parent

<div class="row-fluid" ng-repeat="message in messages.current|filter:'draft'">      {{ message.subject }} ... {{ $parent.campaign.name }} ... </div> 
like image 148
Mathew Berg Avatar answered Sep 19 '22 20:09

Mathew Berg