Skip to content

Instantly share code, notes, and snippets.

@endorama
Last active October 11, 2018 07:28
Show Gist options
  • Save endorama/7369006 to your computer and use it in GitHub Desktop.
Save endorama/7369006 to your computer and use it in GitHub Desktop.
AngularJS 1.2.0 lazy load script in partials
/*
* Angular LoadScript
*
* Let angular load and execute lazy javascript from partials!
*
* This module is the result of this issue: "1.2.0rc1 regression: script tags not loaded via ngInclude"
* Issue url: https://github.com/angular/angular.js/issues/3756
*
* As of Angular 1.2.0 the ngInclude scripts does not permit execution of javascript from included partials.
* This little module execute code inside script tags with "javascript-lazy" attribute after partial loading,
* thus re-enabling this feature.
*
* ( please have a look at the issue comments, this angular feature was never planned nor included properly,
* was only a drawback of using jQuery for partial inclusion )
*
* This angular module have been created by @endorama (https://github.com/endorama) based upon the code
* posted by @olostan (https://github.com/olostan)
*
* Simply add this file, load ngLoadScript module as application dependency and use type="text/javascript-lazy"
* as type for script you which to load lazily in partials.
*
* License: 2013 - released to the Public Domain.
*/
/*global angular */
(function (ng) {
'use strict';
var app = ng.module('ngLoadScript', []);
app.directive('script', function() {
return {
restrict: 'E',
scope: false,
link: function(scope, elem, attr) {
if (attr.type=='text/javascript-lazy') {
var code = elem.text();
var f = new Function(code);
f();
}
}
};
});
}(angular));
@gouravsood
Copy link

Thanks a lot @endorama for sharing this module. This helped me to solve the webflow.js load issue with angularjs (http://forum.webflow.com/t/use-of-web-app-frameworks-like-angular/3191/10).

Thanks,
Gourav

@m24927605
Copy link

Sorry,please tell me how can I load webflow.js with Angular4?Is there any solution?

@DoanVanThuong
Copy link

@m24927605 getting error like same you, have you resolve it??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment