Fai Zal Dong
Fai Zal Dong

Reputation: 247

Use angular expression outside ng-repeat

I know this question has been asked for many times, but most of the answer give the idea to use controller. For my question, I don't want to use any controller, only at view.

I have this on my view:

<p data-ng-repeat="ticketA in cart.sw_tickets" ng-if="ticketA.ProductId == tickets.ProductId && ticketA.PricingTypeId == 1" data-ng-init="adulttotTicket = ticketA.total_ticket">{{adulttotTicket}}</p>

I want adulttotTicket can be use after ng-repeat, supposedly after element <p></p>. I have tried this:

<p data-ng-repeat="ticketA in cart.sw_tickets" ng-if="ticketA.ProductId == tickets.ProductId && ticketA.PricingTypeId == 1" data-ng-init="adulttotTicket = ticketA.total_ticket"></p>{{adulttotTicket}}

But seems like I can't get this value adulttotTicket.

note* I don't want use any controller

Hope anyone can help me, thanks!

Upvotes: 0

Views: 96

Answers (2)

Sangharsh
Sangharsh

Reputation: 3029

Assign it to $rootScope.

Working Plunkr: https://plnkr.co/edit/GtDNj7BRgb5M1MAb3u5V?p=preview

Upvotes: 1

Danilo Velasquez
Danilo Velasquez

Reputation: 501

The variable ticketA is only available inside the scope of ng-repeat. So, in your case i would use some nested divs to handle the problem.

<div data-ng-repeat="ticketA in cart.sw_tickets">
   <div ng-if="ticketA.ProductId == tickets.ProductId && ticketA.PricingTypeId == 1" data-ng-init="adulttotTicket = ticketA.total_ticket">
      <p>{{ticketA}}</p> {{adulttotTicket}}
   <div>
</div>

Upvotes: 0

Related Questions