Skip to content

Instantly share code, notes, and snippets.

@pyrobot
Last active December 16, 2015 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrobot/5486002 to your computer and use it in GitHub Desktop.
Save pyrobot/5486002 to your computer and use it in GitHub Desktop.
injecting angular elements from strings of html
<!DOCTYPE html>
<html ng-app>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular.min.js"></script>
</head>
<body>
<script>
var
domString = '<div>{{1+2}}</div>',
injector = angular.injector(['ng']),
$compile = injector.get('$compile'),
$rootScope = injector.get('$rootScope'),
$scope = $rootScope.$new(),
dom = $compile(domString)($scope),
body = angular.element(document.body);
$scope.$digest();
body.append(dom);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment