Skip to content

Instantly share code, notes, and snippets.

@pentaphobe
Created November 15, 2016 06:18
Show Gist options
  • Save pentaphobe/a810c4fbce63fc715aa4e8e1195552ef to your computer and use it in GitHub Desktop.
Save pentaphobe/a810c4fbce63fc715aa4e8e1195552ef to your computer and use it in GitHub Desktop.
Filter function for Angular's jqlite element
/**
* Adds a filter() method to Angular's jqlite
*/
(function (angular) {
/**
* Polyfill from MDN (mostly to cover IE9 and edge browsers)
* https://developer.mozilla.org/en/docs/Web/API/Element/matches
*/
if (!Element.prototype.matches) {
Element.prototype.matches =
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector ||
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) {}
return i > -1;
};
}
/**
* END OF MDN Polyfill
*/
/**
* Does the actual work of providing a basic selector filter
*/
angular.element.prototype.filter = function (selector) {
return angular.element(
Array.prototype.filter.call(this, function (el) {
return el && (el instanceof Element) &&
el.nodeType === 1 &&
el.matches(selector);
})
);
};
}(angular));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment