简体   繁体   English

带Qtip的角度动态模板

[英]Dynamic template in angular with qtip

To display some user information I am using a directive that captures the click event to display a qTip tooltip. 为了显示一些用户信息,我正在使用一个指令,该指令捕获click事件以显示qTip工具提示。 As I am already using angular, to render the HTML I am using $compile to render the HTML. 因为我已经在使用angular渲染HTML,所以我在使用$ compile渲染HTML。

.directive('myDirective', function ($compile, $timeout) {
    return {
        restrict: 'AE',
        link: function (scope, element, attrs) {
            // Open qTip popup
            element.qtip({
                content: {
                    ajax: {
                        url: '/myurl',
                        type: 'GET',
                        dataType: 'json',
                        success: function (data, status) {
                            // Contact full name
                            scope.FullName = data.contact.Forename + ' ' + data.contact.Surname;

                            var myelement = angular.element('<p>[[ FullName ]]</p>');
                            var compiled = $compile(myelement)(scope);

                            this.set('content.text', compiled)
                        }
                    }
                },
                show: 'click',
                style: {
                    classes: 'qtip-bootstrap'
                },
                hide: {
                    event: 'unfocus'
                }
            });
        }
    };
});

The problem is that the HTML is only rendered some times while in other cases I can see the angular tags coming. 问题在于HTML仅渲染了几次,而在其他情况下,我可以看到角度标记即将到来。 It seemed that compiler output was not rendered at time. 似乎编译器输出未在某个时间呈现。 To solved, I found a solution using a timeout like it shows in the code below. 为了解决这个问题,我找到了一个使用超时的解决方案,如下面的代码所示。

...
success: function (data, status) {
  // Contact full name
  scope.FullName = data.contact.Forename + ' ' + data.contact.Surname;

  var myelement = angular.element('<p>[[ FullName ]]</p>');
  var compiled = $compile(myelement)(scope);

  $timeout(this.set('content.text', compiled), 100); // delay 100 ms
}
...

My problem is that I do not find this solution very efficient and I would like to know if there is a better alternative to this approach. 我的问题是,我认为该解决方案不是很有效,我想知道是否有更好的替代方法。 Any help would be appreciated. 任何帮助,将不胜感激。

$timeout(this.set('content.text', compiled), 100);

Your timeout function is wrong, as it will execute immediately. 您的超时功能是错误的,因为它将立即执行。 But it works because the missing part is to start an angular digest process to update the GUI, because qTip is running outside the angular lifecycle. 但这是可行的,因为缺少的部分是启动角度digest过程以更新GUI,因为qTip在角度生命周期之外运行。 $timeout will implicitly call the digest function. $timeout将隐式调用digest函数。

Try this: 尝试这个:

this.set('content.text', compiled)l
scope.$digest();

Correct implementation of your timeout function: 正确执行您的超时功能:

var that = this;
$timeout(function() {
    that.set('content.text', compiled);
}, 100);

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

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