简体   繁体   English

Bootstrap popover指令逻辑在AngularJS自定义指令中不起作用

[英]Bootstrap popover directive logic not working inside AngularJS custom directive

Setup: angularjs v1.2.3, ui-bootstrap tpl 0.4.0 设置: angularjs v1.2.3,ui-bootstrap tpl 0.4.0

Problem: Directive "popover" from UI Bootstrap inside Custom directive "milestone". 问题:自定义指令“里程碑”中的UI Bootstrap指令“popover”。 Milestone directive gets rendered including the popover tag, but popover logic does not work - showing and hiding the popover. Milestone指令被渲染,包括popover标记,但是popover逻辑不起作用 - 显示和隐藏popover。 Tried to compile the popover HTML before handing over to scope variable milestonesHTML but didn't work. 试图在移交到范围变量里程碑HTML之前编译popover HTML但是没有用。 Any ideas? 有任何想法吗?

Code : (very minimalistic to reduce complexity) 代码 :(非常简约以降低复杂性)

//Controller Variable
$scope.milestonesHTML;

//Usage in HTML
<milestone>
</milestone>

//Directive definition
directive('milestone', function( $compile ) {
return {
    restrict: 'E',

    template: '<span class="test" ng-bind-html="milestonesHTML"></span>',

    link: function(scope, iElement, iAttrs) {
        var milesHtml = '<img popover="End-to-end support" width="20" height="20" src="./img/info.png"/>';
        var compiledMilesHtml = $compile(milesHtml)(scope);
        scope.milestonesHTML = compiledMilesHtml[0].outerHTML;
    }
};

Plunkr Plunkr

I have made a Plunkr for that, see here 我为此制作了一个Plunkr,请看这里

In your link function, you are compiling and linking the img where the popover directive is used. 在链接函数中,您正在编译和链接使用popover指令的img

var milesHtml = '<img popover="End-to-end support" width="20" height="20" src="./img/info.png"/>';
var compiledMilesHtml = $compile(milesHtml)(scope);

This hooks up DOM with events and scope and gives you a final node: compiledMilesHtml . 这将DOM与事件和范围挂钩,并为您提供最终节点: compiledMilesHtml You are then abandoning all of this and binding just the HTML into your displayed DOM 然后,您放弃所有这些并将HTML绑定到显示的DOM中

template: '<span class="test" ng-bind-html="milestonesHTML"></span>',

scope.milestonesHTML = compiledMilesHtml[0].outerHTML;

This is just text, and has no knowledge about how events are hooked up or what watchers on the scope or other workings should be doing to your element. 这只是文本,并且不知道如何连接事件或者对范围或其他工作的观察者应该对元素做什么。 All that remains is the original DOM transformations. 剩下的就是原始的DOM转换。

If you do need to compile the node manually, you can do so and insert the actual element, compiledMilesHtml , into the DOM with jQuery or jQLite. 如果确实需要手动编译节点,可以这样做,并使用jQuery或jQLite将实际元素compiledMilesHtml插入到DOM中。 However, your template is already being compiled, linked and inserted into the DOM. 但是,您的模板已经被编译,链接并插入到DOM中。 Without any other special requirements, your img should just be placed in your template, where it will work fine. 没有任何其他特殊要求,您的img应该只放在您的模板中,它可以正常工作。

template: '<span class="test"><img popover="End-to-end support" width="20" height="20" src="./img/info.png"/></span>'

Demo here . 在这里演示。 I have changed the attributes passed to the popover to match in the custom directive and HTML versions. 我已经更改了传递给popover的属性以匹配自定义指令和HTML版本。

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

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