简体   繁体   English

Angularjs 使用 jquery ui 手风琴 - 时间问题

[英]Angularjs to use jquery ui accordion - timing issue

I want to use Jquery UI's accordion in AngularJS.我想在 AngularJS 中使用 Jquery UI 的手风琴。 So i wrote a directive:所以我写了一个指令:

angular.module('accordion', [])
    .directive('accordion', function () {
        return {
            restrict: 'AE',
            replace: true,
            link: function (scope, element, attrs) {
                $(document).ready(function () {
                    setTimeout(function () {
                        element.accordion({ icons: false });
                    }, 1000);
                });
            }
        };
    });

And I used the setTimeout because the directive is loaded before the page (or is it? I'm not sure), and so I need the timeout for it to be loaded.我使用 setTimeout 是因为指令是在页面之前加载的(或者是吗?我不确定),所以我需要超时才能加载它。 Of course the timeout is not always sufficient, and sometimes it is too quickly so I can see the HTML before the directive effect it, and then it changes to accordion and I don't want the users to see it.当然,超时并不总是足够的,有时它太快了,所以我可以在指令生效之前看到 HTML,然后它变成手风琴,我不希望用户看到它。

Ideas?想法?

Solved it... Added a watch in the directive - that watch a variable that changes when the data recieives from the server - and the accordion is activated!解决了...在指令中添加了一个监视 - 监视一个变量,该变量在从服务器接收数据时发生变化 - 并且手风琴被激活!

angular.module('accordion', [])
    .directive('accordion', function () {
        return {
            restrict: 'AE',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('serverData', function (newValue, oldValue) {
                    if (oldValue != newValue) {
                        element.accordion();
                    }
                });
            }
        };
    });

you dont need ($document).ready because directives are linked after angular bootstrapping process which in itself happens after the document is ready.你不需要 ($document).ready 因为指令是在 angular bootstrapping 过程之后链接的,这本身发生在文档准备好之后。

Now Im not sure what you are using the accordion with, if its image heavy, or uses other templates, its quite possible that this directive will be linked before all the images and ng-includes have loaded.现在我不确定你在使用手风琴的什么,如果它的图像很重,或者使用其他模板,很可能这个指令将在所有图像和 ng-includes 加载之前链接。

Can you create plunker with an example?你能用一个例子创建plunker吗?

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

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