0

I have to use jQuery just because ng-grid needs it. But now I have some problem. In directive, when I try to use angular's element, function returns me jQuery element. But if I remove jQuery from page, everything works just fine.

Is there any way to correct this?

app.directive('dynamic', function($compile) {
    return {
        restrict: 'A',
        replace: true,
        link: function(scope, ele, attrs) {
            scope.$watch(attrs.dynamic, function(html) {
                console.log(ele) //HERE
                ele.html(html);
                $compile(ele.contents())(scope);
            });
        }
    };
})

When I have jQuery, element looks like this:

[div, context: div, jquery: "1.11.0", constructor: function, selector: "", toArray: function…]

Otherwise:

[div, ready: function, toString: function, eq: function, push: function, sort: function…]

(I know it is not the full array, but you can see the difference).

1 Answer 1

1

Angular has jqlite, which is a small subset of jQuery. It is used when jQuery isn't loaded on the page or is loaded after angular. Otherwise, angular will use jQuery instead of jqlite (but will still wrap it and add extra methods such as .scope() that jqlite has).

See documentation: https://docs.angularjs.org/api/ng/function/angular.element

So to answer your question, this behavior makes sense.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I get it, but my problem here is unsuccessful compiling, when I have whole jQuery on page -> $compile(ele.contents())(scope);
And it happens only when jquery is enabled? Any chance of a plunker that shows it?

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.