dynamic_directive_srv.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import angular from 'angular';
  2. import coreModule from '../core_module';
  3. class DynamicDirectiveSrv {
  4. /** @ngInject */
  5. constructor(private $compile) {}
  6. addDirective(element, name, scope) {
  7. const child = angular.element(document.createElement(name));
  8. this.$compile(child)(scope);
  9. element.empty();
  10. element.append(child);
  11. }
  12. link(scope, elem, attrs, options) {
  13. const directiveInfo = options.directive(scope);
  14. if (!directiveInfo || !directiveInfo.fn) {
  15. elem.empty();
  16. return;
  17. }
  18. if (!directiveInfo.fn.registered) {
  19. console.log('register panel tab');
  20. coreModule.directive(attrs.$normalize(directiveInfo.name), directiveInfo.fn);
  21. directiveInfo.fn.registered = true;
  22. }
  23. this.addDirective(elem, directiveInfo.name, scope);
  24. }
  25. create(options) {
  26. const directiveDef = {
  27. restrict: 'E',
  28. scope: options.scope,
  29. link: (scope, elem, attrs) => {
  30. if (options.watchPath) {
  31. let childScope = null;
  32. scope.$watch(options.watchPath, () => {
  33. if (childScope) {
  34. childScope.$destroy();
  35. }
  36. childScope = scope.$new();
  37. this.link(childScope, elem, attrs, options);
  38. });
  39. } else {
  40. this.link(scope, elem, attrs, options);
  41. }
  42. },
  43. };
  44. return directiveDef;
  45. }
  46. }
  47. coreModule.service('dynamicDirectiveSrv', DynamicDirectiveSrv);