1

Suppose a jasmine test exposing this code in the beforeEach block:

//....
element = $compile('<input class="form-control" class="email" name="email" id="email" type="email" ng-model="user.email" />')($rootScope);
$rootScope.$digest();
//.....

In some test, I put a simple:

console.log($('#email').name);

expecting thus the output of the value: email.

However, I get an undefined.

So my question is: Is jquery compatible by default (without jasmine-jquery plugin or anything else)? Of course, in my karma configuration, jquery is loaded before angularjs.

Of course, passing by the traditional element.find(...) works well (although does only work with tagnames) , but I would like to use Jquery selector instead.

1 Answer 1

5

The jQuery id selector uses document.getElementById(). Your compiled element is not part of the DOM, which is why console.log($('#email').name); gives undefined.

If you have a single element like in your example you simple need to do this:

console.log(element.attr('name'));

And as long as you have jQuery loaded before AngularJS find will work with more than tag names. So if you have a nested element:

var nestedElement = $compile('<div><input class="form-control" class="email" name="email" id="email" type="email" ng-model="user.email" /></div>')($scope);

You can:

console.log(nestedElement.find('#email').attr('name'));

Working example: http://plnkr.co/edit/KRmoWRq8efvb4QhwDXAX?p=preview

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

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.