James
James

Reputation: 1

Setting a variable in parent div and using it in children in angular

I'm new to AngularJS. I'm developing a page with a couple of similar blocks but I don't really want to use ng-repeat for those. The thing is, in their ng-click, ng-class and other directives I need to use some variable identifying the block. Is there a way to set it once in parent div and then use in children? Something like this:

<div ng-var="Potatoes">
    <button ng-click="buy($parent.var)">Buy</button>
    <span>This is a {{$parent.var}}</span>
    <img ng-class="{{$parent.var}}">
</div>

<div ng-var="Tomatoes">
    <button ng-click="buy($parent.var)">Buy</button>
    <span>This is a {{$parent.var}}</span>
    <img ng-class="{{$parent.var}}">
</div>

I would use ng-repeat over a specifically crafted object but in this case I have to manually position some other things that I omitted in the code above so ng-repeat is not an option, unfortunately.

Upvotes: 0

Views: 108

Answers (1)

Karthikvenkat86
Karthikvenkat86

Reputation: 84

You can still use 'ng-repeat' then use of '$index' can differentiate each loop.

Upvotes: 0

Related Questions