0

I have some jquery code using $ in my controller. When I include jquery in the page, then it works fine but when I remove jQuery then I get following error:

ReferenceError: $ is not defined

I read online that jquery has inbuilt small footprint jquery named jQlite then why I get the error:

$('.scrollProductPicker , .scrollTailoredSupport').click(function(){
        $('html, body').animate({
            scrollTop: $( $(this).attr('href') ).offset().top
        }, 500);
        return false;
    });
3
  • Angular uses jQLite. jQLite is enough for angular to work. if you are using any jquery code you should include it Commented Jun 18, 2015 at 4:38
  • Please do check if you have some JQuery functionality in your page. Commented Jun 18, 2015 at 4:39
  • If jQuery is available, angular.element is an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite.". Source Commented Jun 18, 2015 at 4:40

4 Answers 4

2

in Angular JS angular.element is used as an alias for $ or "JQuery", which is part of jqlite and hence if you are using $ directly you need to add the reference of jQuery. more details you can find at this link Angular JS documentation

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

Comments

1

Looking up elements via selectors is not supported by jqLite.

jLite is avaiable here angular.element.

So you cannot do this:

angular.element("body"); //DOES NOT WORK!

However, you can do:

angular.element(document.body); //DOES WORK!

jLite has just a sub set of functions from jQuery.

Comments

1

@Ramesh & @ThiagoPXP has given the correct answers. But you can still make full use of Angular to select any elements like so:

angular.element(document.querySelectorAll("div.foo"));
// or
angular.element(document.querySelector("#my-page ul.bar"));

You can make your code simpler by registering them as methods in Angular:

angular.findAll = function(selector) {
    return angular.element(document.querySelectorAll(selector));
}

And then you can write easily as:

angular.findAll("div.foo");
// or
angular.findAll("#my-page ul.bar");

Or simpler (to use syntax like jQuery):

window.$ = function(selector) {
    return angular.element(document.querySelectorAll(selector));
}

$("#my-page ul.bar") // will work

But, if you do not include jQuery, Angular will use a small version of jQuery i.e. jqlite which will not have every method available as jQuery.

Comments

0

This is definitely good. but I want that smooth scroll feature. Couldn't use angular-smoothscroll directive as the link is outside of my angular app and the target anchor is inside the angular app. I ended up with using vanilla javascipt for smooth scroll

Comments

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.