Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directive after ngRepeat update

I'm having some AngularJS trouble with firing off a directive when ng-repeat has completed an update. I have three named arrays, switched to via three links, allowing the selected array to be displayed by the ng-repeat. I'd like to fire some code off when it's finished as I'm planning to set some element attributes for D3 to use.

I've tried checking scope.$last in my directive, but this isn't called at the end of every ng-repeat process. If some of the data remains the same, it may not set scope.$last to true.

http://plnkr.co/edit/hwmOlI6YrgS4H1C1h7k2?p=preview

So, what's the best way to trigger code in a directive when ng-repeat has finished?

like image 600
Mister Dai Avatar asked Dec 27 '22 06:12

Mister Dai


1 Answers

Here is a solution for you. Note that the last triggers each time now.

http://plnkr.co/edit/xV1quqzorzxliS2shrP4?p=preview

You just need to $watch the $last variable and it will work fine. This helps in situations where the scope is not created but just updated with new values. Your directive gets created once and if one of the repeated variable just changes values ng-repeat optimizes and just updates values ( as opposed to removing all the values and re-creating the new ones. ). In this scenario, the $scope.$last will be an updated variable and not something that gets "created". So, you will need to $watch it.

like image 157
ganaraj Avatar answered Jan 25 '23 19:01

ganaraj