I have a simple directive where the template uses ng-repeat inside it. I need to run some code to instantiate a jquery component against some of the elements created by the ng-repeat directive. The problem is that if I put this code in the link function. The ng-repeat hasn't built those elements yet so nothing is instantiated.
App.directive('myDirective', ['$compile', '$timeout', function($compile, $timeout) {
return {
scope: {
domains: '='
},
templateUrl: '/app/partials/my_directive.html',
link: function($scope, element, attributes) {
element.find('.standard-help').tooltip('destroy');
element.find('.standard-help').tooltip({placement: 'top', trigger: 'click hover focus'});
}
};
}
The template would look like the following. I'm trying to attach
<ul class="media-list domainList">
<li class="media" style="position:relative;" ng-repeat="domain in domains">
<a class="domainHeader" href="javascript://">
<span class="domainHeader">{{domain.tag}}</span>
</a>
<div class="media-body" style="margin-left: 52px;">
<ul class="standardsList">
<li ng-class="{ standardDisplayed: lessonLayout == 'standards' }" ng-hide="standard.lessons.length == 0" ng-repeat="standard in domain.standards">
<a href="javascript://" title="{{standard.description}}" ng-show="lessonLayout == 'standards'" class="standard-help pull-right"><i class="icon-question-sign"></i></a>
<h6 ng-show="lessonLayout == 'standards'">{{standard.tag}}</h6>
<ul class="lessonsList">
<li ng-class="{ lesson: true }" ng-repeat="lesson in standard.lessons" ng-click="onLessonSelected(lesson)">
<i class="icon-lock lesson-locked"></i>
<div>{{lesson.title}}</div>
</li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
I've tried using $watch() and $observe() to register a callback when the domains change and instantiate the tooltip code then. However, I can't seem to get it to call me at the right time. Any ideas what I'm missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…