Skip to content

Instantly share code, notes, and snippets.

@TheRusskiy
Created January 18, 2015 23:51
Show Gist options
  • Save TheRusskiy/ffd658167125969ae5dd to your computer and use it in GitHub Desktop.
Save TheRusskiy/ffd658167125969ae5dd to your computer and use it in GitHub Desktop.
Parse HTML generated outside of Angular
var $injector = angular.element(document).injector(); // first we get the injector our app uses
$injector.invoke(function($rootScope, $compile) { // you inject anything here in params
var element = angular.element('#selector'); // get the element we want to parse
// following hack isn't always neccessary, but I use to prevent double parsing if the same HTML is generated statically and dynamically
if (element.attr('class') && element.attr('class').indexOf("ng-scope")!=-1){
return;
}
var compiled = $compile(element)($rootScope); // finally, we compile the element against root scope
compiled.scope().$apply(); // ... and we tell Angular that we made changes outside of its 'world'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment