-
-
Save culttm/f3979c00605d138cfdd0 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 (…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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