我有一个简单的指令,模板在其中使用ng-repeat。我需要运行一些代码来针对由ng-repeat指令创建的一些元素实例化jquery组件。问题是,如果我把这个代码放在链接函数中。 ng-repeat尚未构建这些元素,因此没有实例化。AngularJS:链接到使用ng-repeat的指令中的元素
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'});
}
};
}
该模板如下所示。我试图连接
<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>
我使用$表()和$观察尝试()时域改变和实例工具提示代码,然后注册一个回调。但是,我似乎无法在正确的时间给我打电话。任何想法我失踪?
也许你将迫使链接功能编译。 element = angular.element($ compile(template)(scope)); – dimirc
如果问题正在等待dom渲染,则可以将链接函数项目包装在$ timeout中。 '$超时(函数(){element.find(布拉布拉); element.find(foofoo);},0)'。如果这不是修复,那么问题不在于dom渲染。 – rGil
似乎这将是一个普遍的问题,所以我不得不认为有更好的方法来实现这一点,而不诉诸像使用超时黑客。 – chubbsondubs