Skip to content

Instantly share code, notes, and snippets.

@ilguzin
Last active August 29, 2015 14:05
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 ilguzin/97a153c18a01b64f6541 to your computer and use it in GitHub Desktop.
Save ilguzin/97a153c18a01b64f6541 to your computer and use it in GitHub Desktop.
How to load script inside AngularJS partial. Note [SCRIPT_PATH] in example.html
<script type="text/javascript" data-ng-script-load>
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '[SCRIPT_PATH]';
document.getElementsByTagName('head')[0].appendChild(script);
</script>
define ["angular"], (angular) ->
'use strict'
ngScriptLoad = () ->
restrict: "E"
scope: false
link: (scope, elem, attr) ->
if angular.isDefined(attr.ngScriptLoad)
code = elem.text()
f = new Function(code)
f()
return
[ngScriptLoad]
define [
"angular"
"./directives/ngScriptLoadDirective"
], (
angular,
ngScriptLoad,
) ->
"use strict"
angular.module("smapp.main", [])
.directive("script", ngScriptLoad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment