[英]How to initialize Bootstrap tooltip in custom directive
I'm trying to move my tooltip initialization out of jQuery and into the link function of a custom directive. 我试图将我的工具提示初始化从jQuery移到自定义指令的链接函数中。 The HTML is fairly simple:
HTML非常简单:
<p data-toggle='tooltip' data-placement='bottom' title='Players live: {{players.loggedIn}}' id='login-count'>
Current jQuery: 当前的jQuery:
$(document).ready(function() {
$('#login-count').tooltip();
});
What I'm attempting to do in my custom directive: 我要在自定义指令中尝试执行的操作:
function link(scope, element) {
var loginCount = angular.element(element[0].querySelector('#login-count'));
angular.element(document).ready(function() {
loginCount.tooltip();
});
}
However, I'm getting the following error: Uncaught TypeError: loginCount.tooltip is not a function
但是,我收到以下错误:
Uncaught TypeError: loginCount.tooltip is not a function
your custom directive should be like this 您的自定义指令应如下所示
angular.module('yourAppName') .directive('mytooltip', function() {
'use strict';
return {
restrict: 'A',
link: function(scope, el) {
$(el).tooltip();
}
};
});
and then i html you can add it like: 然后我HTML您可以添加它像:
<p data-toggle='tooltip' data-placement='bottom'
title='Players live: {{players.loggedIn}}' id='login-count' mytooltip>
also keep in mind to include jquery before angular.js because otherwise Angular will load and use jqlite. 还请记住在angular.js之前包含jquery,因为否则Angular将加载并使用jqlite。
In any case I would rather go with Angular tooltips like http://720kb.github.io/angular-tooltips or even better bootstrap for angular http://angular-ui.github.io/bootstrap 无论如何,我宁愿使用诸如http://720kb.github.io/angular-tooltips之类的Angular工具提示,或者甚至更好的角度角度http://angular-ui.github.io/bootstrap引导程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.