Created
January 18, 2015 23:51
Parse HTML generated outside of Angular
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
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