Skip to content

Instantly share code, notes, and snippets.

@thers
Last active March 10, 2016 08:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thers/7525451 to your computer and use it in GitHub Desktop.
Save thers/7525451 to your computer and use it in GitHub Desktop.
When you need to initialize jquery plugin when using AngularJS (or something else which dinamically includes html). In case of AngularJS i was needed to render user-generated articales which contains HTML code inside of AngularJS app using $sce.trustAsHtml(), but the thing is about directives dont work inside this HTML and it shouldn't in fact (…
/**
* Executes given callback only if some element found by given selector
*
* @param {String} selector
* @param {Function} cb
*/
window.anyway = function (selector, cb) {
var
interval = setInterval(function () {
var el = $(selector);
if(el.length !== 0) {
cb(el);
clearInterval(interval);
}
}, 1); // Chrome acts like fcking bastard and minimum interval delta can be only 4ms but anyway let's set 1
setTimeout(function () {
clearInterval(interval);
}, 500); // Hope it will be enough
};
/**
* Usage
*
* Somewhere in your AngularJS controller or somewhere else
*/
$scope.$on('$viewContentLoaded', function () {
anyway('.fotorama', function (elements) {
elements.fotorama();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment