AngularLoader.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import angular from 'angular';
  2. import { assign } from 'lodash';
  3. import { AngularComponent, AngularLoader as AngularLoaderInterface } from '@grafana/runtime';
  4. import { GrafanaRootScope } from 'app/angular/GrafanaCtrl';
  5. import coreModule from 'app/angular/core_module';
  6. export class AngularLoader implements AngularLoaderInterface {
  7. /** @ngInject */
  8. constructor(private $compile: any, private $rootScope: GrafanaRootScope) {}
  9. load(elem: any, scopeProps: any, template: string): AngularComponent {
  10. const scope = this.$rootScope.$new();
  11. assign(scope, scopeProps);
  12. const compiledElem = this.$compile(template)(scope);
  13. const rootNode = angular.element(elem);
  14. rootNode.append(compiledElem);
  15. return {
  16. destroy: () => {
  17. scope.$destroy();
  18. compiledElem.remove();
  19. },
  20. digest: () => {
  21. if (!scope.$$phase) {
  22. scope.$digest();
  23. }
  24. },
  25. getScope: () => {
  26. return scope;
  27. },
  28. };
  29. }
  30. }
  31. coreModule.service('angularLoader', AngularLoader);