I am trying to lazy load the external script (in my case ckeditor) in a directive only when its needed.
HTML Template - directives/myEditor.tmplt
<div>
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
</div>
Angular Module - MyEditorModule
define(['angular'], function(angular){
var moduleName = 'MyEditorModule';
angular
.module(moduleName)
.directive('myEditor',function($scope){
return {
restrict: 'E',
templateUrl: 'directives/myEditor.tmplt',
link: function(scope, element){
scope.CKEDIOTR_LOADED = false;
require([
'https://cdn.ckeditor.com/4.4.7/standard/ckeditor'
], function(){
scope.CKEDIOTR_LOADED = true;
CKEDITOR.replace('editor1');
/**
* Registering other actions for CKEDITOR
*
**/
});
}
};
});
return moduleName;
});
I have some doubts
- is it safe to load scripts like this inside controller or link function ?
- do i have to trigger $apply or $digest inside CKEditor's require callback function ?
- is there any better and cleaner way of doing this kind of lazy loading ?
ng-repeat
and the like. I think this might be premature optimization frankly. What is leading you to decide to do this? In any case, Angular does not have support for per-directive lazy loading.