Шаблон извлечения компонента Angularjs не скомпилирован - PullRequest
0 голосов
/ 31 октября 2019

У меня есть следующее

<my-component react="elmt.hasChanged">...</my-component>

my-component имеет собственный шаблон и правильно отображает

react выглядит так:

export default angular
  .module('app.react', [])
  .directive('react', ['$compile', function ($compile) {
    'use strict';
    return {
      restrict: 'A',
      link: function link(scope, $el, attrs) {
        scope.$watch(
          function (scope) {
            // watch the 'compile' expression for changes
            return scope.$eval(attrs.react);
          },
          function (newval, oldval) {
            if (!!newval && newval !== oldval) {
              const elm = angular.element(require('./my-component.tpl.html')); //Get the element
              $el.html(elm); // append the element to DOM
              $compile(elm)(scope); //Now compile it to render the directive.
              scope.$eval(attrs.react + '=false;');
            }
          }
        );
      }
    };
  }]).name;

И чтоя хотел бы заменить часть const elm = angular.element(require('./my-component.tpl.html'));, динамически загружая шаблон компонента, в котором объявлена ​​директива.

Цель всего этого состоит в том, чтобы my-component имел loooooot статических привязок, которые должныобновляться при изменении elmt.hasChanged, поэтому, чтобы избежать слишком большого количества наблюдателей, я перекомпилирую / повторно приложу шаблон к DOM.

Возможно ли это?

...