Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS DOM / document selections without jQuery
    primarykey
    data
    text
    <p>AngularJS doesn't have a built in DOM selection engine yet provides utility methods that handle a portion of what jQuery would provide for a typical application.</p> <p>However, DOM selection is still king and I'm trying to keep jQuery out of my application for the sole purpose of DOM selections.</p> <p>Angular provides a $document service as an injectable. I find this to be cumbersome. For example, to achieve native DOM selections using $document you would need to inject $document into all directives desired and call querySelectorAll, then extend it with Angular:</p> <pre><code>angular.element( $document[0].querySelectorAll('#element') ) </code></pre> <p>This is silly. </p> <p>In there interim I've provided myself a global helper, but it doesn't use Angular's wrapped $document...</p> <pre><code> // Create a shortcut to querySelectorAll window.query = function(selector) { return document.querySelectorAll( selector ) } // For use inside a directive like so... angular.element( query('#element') ) </code></pre> <p>AngularJS is ALLERGIC to globals and wraps globals for protection and testability. Is there a clean way I can leverage querySelectAll without passing $document into every directive? Can I extend angular.element in the same fashion jQuery does but utilize querySelectorAll?</p> <p><strong>Edit:</strong></p> <p>I also want to note the original JQLite library supports basic selectors <a href="https://code.google.com/p/jqlite/wiki/UsingJQLite#Supported_Selectors" rel="nofollow">https://code.google.com/p/jqlite/wiki/UsingJQLite#Supported_Selectors</a>.</p> <p>The AngularJS implementation however does not support selectors nor appears to mention this feature omission - <a href="http://docs.angularjs.org/api/angular.element" rel="nofollow">http://docs.angularjs.org/api/angular.element</a></p> <p>Instead AngularJS does:</p> <pre><code>throw Error('selectors not implemented'); </code></pre> <p>^ when the item passed to angular.element is a string which does not start with '&lt;'.</p> <p>It'd be nice if that was:</p> <pre><code>return document.querySelectorAll( element ) </code></pre> <p>...with some basic error prevention logic.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload