简体   繁体   English

模板中的AngularJS + Bootstrap工具提示

[英]AngularJS + Bootstrap tooltip in template

I have a template created by Rails. 我有一个由Rails创建的模板。 Putting a Bootstrap tooltip in the application.html.erb works fine but putting one in the template partial loaded by AngularJS does not work. 将Bootstrap工具提示放入application.html.erb可以正常工作,但是将一个工具提示放入AngularJS局部加载的模板中则不起作用。

application.js.coffee application.js.coffee

jQuery ->
    $("a[rel~=popover], .has-popover").popover()
    $("a[rel~=tooltip], .has-tooltip").tooltip()

application.html.erb application.html.erb

<a href="#" class="has-tooltip" title="tooltip">hover over me for a tooltip</a>

The above works on any page as you'd expect. 上面的代码可以在您期望的任何页面上运行。

/app/assets/templates/guides/show.html.erb /app/assets/templates/guides/show.html.erb

<a href="#" class="has-tooltip" title="tooltip">hover over me for tooltip </a>

The above template is being loaded by the AngularJS "ng-view" directive in the application.html.erb file thus: 上面的模板是由application.html.erb文件中的AngularJS“ ng-view”指令加载的,因此:

<div id="ng-app" ng-app="Guide" ng-view >
    <!-- angular templates load here -->
</div>

The appropriate AngularJS route is triggering the load here: 适当的AngularJS路由在此处触发加载:

when('/guides/:id', {
    templateUrl:'<%= asset_path("guides/show.html") %>',
    controller: 'VideoDetailCtrl'
});

When displaying a page with both links, the application.html.erb supplied tooltip works but the one in show.html.erb does not. 当显示带有两个链接的页面时,application.html.erb提供的工具提示有效,但show.html.erb中的提示无效。

Seems like a timing problem. 似乎是时间问题。 The load and attachment of the first tooltip happens fine but before the AngularJS template or partial is loaded. 第一个工具提示的加载和附件进行得很好,但是在AngularJS模板或部分加载之前。 Somehow I need to trigger the attachment of the tooltip templates and partials are loaded, right? 我需要以某种方式触发工具提示模板的附件,并加载部分文件,对吗?

This blog post talks about a method to attach jQuery in partials that seems rather baroque. 这篇博客文章讨论了将jQuery附加到局部中的方法,这似乎很巴洛克。 It uses onload in an ng-include to replace ng-view . 它在ng-include使用onload替换ng-view

http://blog.ruedaminute.com/2012/02/angular-js-applying-jquery-to-a-loaded-partial/ http://blog.ruedaminute.com/2012/02/angular-js-applying-jquery-to-a-loaded-partial/

I wonder if there is a more general Angular way to do this especially since tooltip is only one of many things that will need to be attached to the final DOM? 我想知道是否有更通用的Angular方法可以做到这一点,特别是因为工具提示只是需要附加到最终DOM的众多事物之一?

Actually Angular-UI is the answer. 实际上, Angular-UI就是答案。 Silly me, I was using the same markup as standard Bootstrap and the same script. 愚蠢的我,我使用的是与标准Bootstrap相同的标记和相同的脚本。 All I did was include ui.boostrap from Angular-UI and change the markup to: 我所做的只是包括来自Angular-UI的ui.boostrap并将标记更改为:

<a href="" tooltip-placement="right" tooltip="On the Right!">angular-ui tooltip</a>

In your app.js or wherever you add module dependencies you need to put: 在app.js或添加模块依赖项的任何位置,都需要放置:

angular.module('myModule', ['ui.bootstrap']); 

BTW you may need to back up to 0.5.0 of the ui-bootstrap since the 0.6.0 version conflicts with the latest AngularJS (1.2 rc3 at this writing) 顺便说一句,您可能需要备份ui-bootstrap的0.5.0,因为0.6.0版本与最新的AngularJS(在撰写本文时为1.2 rc3)冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM