我是AngularJS的新手,
我正在尝试创建一个将 jQuery JScrollPane (JScrollpane.kelvinluck.com) 应用于 div 的自定义指令。
我有以下html代码:
<scrollPane> ------> WRONG
<div scroll-pane> -----> RIGHT
<ul>
<li data-ng-repeat="task in tasks">
<div class="task-element">
<div class="name">{{task.display_name}}</div>
<div class="text">{{task.min_description}}</div>
</div>
</li>
</ul>
</scrollPane>
和以下指令:
angular.module('myApp', []).directive('scrollPane',function() {
return {
restrict:'A',
template: '<div></div>',
link: function (scope, elm, attrs) {
console.log("in directive");
element.addClass('scroll-pane');
element.jScrollPane({showArrows: true,autoReinitialise:true });
}};
});
主要问题是模板的 html 覆盖了整个<scrollPane>
元素,并且 ng-repeat<li>
根本不显示并从最终的 html 中删除。
创建指令以用 html 包装内部 html 的正确方法是什么(在我的示例中,它将是 div 和class='scroll-pane' )
,我应该在哪里插入 :
$('.scroll-pane').jScrollPane();
?
感谢帮助者。
更新:好吧,上面的代码不起作用,问题出在 HTML 中,因为指令应该在指令中带有一个限制:“A”。(感谢http://www.befundoo.com/university/tutorials/angularjs-directives-tutorial/tips)
要应用 jScrollPane,我只是在指令内使用
element.jScrollPane({showArrows: true,autoReinitialise:true });
一切都为我工作,但我有一个问题悬而未决:对于一些 css 解决方法,我必须在 ng-repeat 中获取任务的长度,以便为 div 设置一个固定的高度。当我在调试时,我看到它还没有渲染,怎么可能等到 ng-repeat 得到它的数据?(使用服务)。