我想手动编译一些包含指令的HTML。 Angular 2中的$compile等价于什么?

例如,在Angular 1中,我可以动态编译HTML片段并将其附加到DOM:

var e = angular.element('<div directive></div>');
element.append(e);
$compile(e)($scope);

最佳答案

2.3.0(2016-12-07)

要获取所有详细信息,请检查:

  • How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

  • 要查看实际效果:
  • 观察a working plunker(使用2.3.0+版本)

  • 负责人:

    1)创建模板
    2)创建组件
    3)创建模块
    4)编译模块
    5)创建(并缓存)ComponentFactory
    6)使用Target创建它的实例

    快速概述如何创建组件
    createNewComponent (tmpl:string) {
      @Component({
          selector: 'dynamic-component',
          template: tmpl,
      })
      class CustomDynamicComponent  implements IHaveDynamicData {
          @Input()  public entity: any;
      };
      // a component for this particular template
      return CustomDynamicComponent;
    }
    

    一种将组件注入(inject)NgModule的方法
    createComponentModule (componentType: any) {
      @NgModule({
        imports: [
          PartsModule, // there are 'text-editor', 'string-editor'...
        ],
        declarations: [
          componentType
        ],
      })
      class RuntimeComponentModule
      {
      }
      // a module for just this Type
      return RuntimeComponentModule;
    }
    

    一个代码片段,如何创建ComponentFactory(并将其缓存)
    public createComponentFactory(template: string)
        : Promise<ComponentFactory<IHaveDynamicData>> {
        let factory = this._cacheOfFactories[template];
    
        if (factory) {
            console.log("Module and Type are returned from cache")
    
            return new Promise((resolve) => {
                resolve(factory);
            });
        }
    
        // unknown template ... let's create a Type for it
        let type   = this.createNewComponent(template);
        let module = this.createComponentModule(type);
    
        return new Promise((resolve) => {
            this.compiler
                .compileModuleAndAllComponentsAsync(module)
                .then((moduleWithFactories) =>
                {
                    factory = _.find(moduleWithFactories.componentFactories
                                    , { componentType: type });
    
                    this._cacheOfFactories[template] = factory;
    
                    resolve(factory);
                });
        });
    }
    

    一个代码片段如何使用以上结果
      // here we get Factory (just compiled or from cache)
      this.typeBuilder
          .createComponentFactory(template)
          .then((factory: ComponentFactory<IHaveDynamicData>) =>
        {
            // Target will instantiate and inject component (we'll keep reference to it)
            this.componentRef = this
                .dynamicComponentTarget
                .createComponent(factory);
    
            // let's inject @Inputs to component instance
            let component = this.componentRef.instance;
    
            component.entity = this.entity;
            //...
        });
    

    带有所有详细信息的完整描述read here,或者观察working example





    关于angular - 等效于Angular 2中的$ compile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34784778/

    10-12 07:12