AngularLoader.ts 1.1 KB

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