0

What would be equivalent expression in AngularJS for jQuery's expression.

Expression

$('.className');

I believe this expression would serve equivalent as above in Angular

angular.element(document.querySelector(".className"))
5
  • 1
    I'm not an angular expert but a simple test shows as if you're right jsfiddle.net/z1kgde9u Commented Sep 12, 2017 at 8:15
  • Did you run a simple test? did you look in angular docs? Commented Sep 12, 2017 at 8:19
  • @fatman yes, i ran the test. I want to access offset().top;. so was finding it in angular... previously i was running $('.className').offset.top. what would be alternative for this expression? Commented Sep 12, 2017 at 9:21
  • well, that's not what you asked is it? Commented Sep 12, 2017 at 9:32
  • @fatman yes, due to this question, my original question was asked. Commented Sep 12, 2017 at 17:22

2 Answers 2

2

AngularJS doesn't have a equivalent per say $("stuffhere") from jQuery. angular.element() simply takes a JavaScript DOM object or jQuery object, so what you posted is correct. Another example is below.

var result = document.getElementsByClassName("class1");
var wrappedAngularResult = angular.element(result);

or

var result = angular.element(".class1");

According to their docs, angular.element is an alias for jQuerys main function if available, if not it defaults to angulars built in lite version jqLite. See here

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

Comments

1

Angular uses so called jQuery lite. It's a subset of jQuery. But if you include a full jQuery on your page then you can use angular.element the same way you use $.

From docs:

If jQuery is available, angular.element is an alias for the jQuery function.

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.