dynamic_directive_srv.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import coreModule from '../core_module';
  4. class DynamicDirectiveSrv {
  5. /** @ngInject */
  6. constructor(private $compile, private $parse) {}
  7. addDirective(element, name, scope) {
  8. var child = angular.element(document.createElement(name));
  9. this.$compile(child)(scope);
  10. element.empty();
  11. element.append(child);
  12. }
  13. create(options) {
  14. let directiveDef = {
  15. restrict: 'E',
  16. scope: options.scope,
  17. link: (scope, elem, attrs) => {
  18. options.directive(scope).then(directiveInfo => {
  19. if (!directiveInfo || !directiveInfo.fn) {
  20. return;
  21. }
  22. if (!directiveInfo.fn.registered) {
  23. coreModule.directive(attrs.$normalize(directiveInfo.name), directiveInfo.fn);
  24. directiveInfo.fn.registered = true;
  25. }
  26. this.addDirective(elem, directiveInfo.name, scope);
  27. }).catch(function(err) {
  28. console.log('Dynamic directive load error: ', err);
  29. });
  30. }
  31. };
  32. return directiveDef;
  33. }
  34. }
  35. coreModule.service('dynamicDirectiveSrv', DynamicDirectiveSrv);