2

I want to use Jquery UI's accordion in AngularJS. 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. 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.

Ideas?

2 Answers 2

2

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();
                    }
                });
            }
        };
    });
Sign up to request clarification or add additional context in comments.

Comments

0

you dont need ($document).ready because directives are linked after angular bootstrapping process which in itself happens after the document is ready.

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.

Can you create plunker with an example?

5 Comments

Removing the $document.ready didn't help... And I load data from the server - so I think it will be pretty hard to simulate this on plunker...
does this problem not appear when you use local data?
Nope.. I can use plain text, with no $document(ready) and no timeout - the directive will only contain the "element.accordion();" line - and it works.
can you explain what you mean by " 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."
In Jquery UI accordion - I use <h3><div> several times - with ng-repeat. So when I use the setTimeOut(1000) I can see the original h3 style and then the div with the data from the ng-repeat, I can see this for a moment - and then the directive timeout passes and it changes to accordion as it should be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.