如何在 angular 中使用innerHtml 绑定/渲染{{...}} html 代码

Sun*_*une 2 http angular

我正在尝试使用 innerHTML 绑定/呈现 html 内容,但无法以角度呈现 {{...}}。

代码如下:

<div [innerHtml]="TestString"></div>

test = " HTML Content ";
TestString = "<div>This is test code, i am trying to bind/render {{ test }} code with angular..,</div>";
Run Code Online (Sandbox Code Playgroud)

结果

这是测试代码,我正在尝试使用 angular 绑定/渲染 {{ test }} 代码..,

不绑定/渲染测试变量值....

Bar*_*ena 7

试试这个方法:

test = " HTML Content ";
TestString = `<div>This is test code, i am trying to bind/render ${this.test} code with angular..,</div>`
Run Code Online (Sandbox Code Playgroud)

https://stackblitz.com/edit/angular-ckxsp8?file=src/app/app.component.ts

如果要从组件内的字符串评估模板,您可以创建自己的指令来执行此操作:

compile.directive.ts

import {
  Compiler, NgModule, Component, Input, ComponentRef, Directive, 
  ModuleWithComponentFactories, OnChanges, Type,
  ViewContainerRef
} from '@angular/core';
import { CommonModule } from '@angular/common';

@Directive({
  selector: '[compile]'
})
export class CompileDirective implements OnChanges {
  @Input() compile: string;
  @Input() compileContext: any;

  compRef: ComponentRef<any>;

  constructor(private vcRef: ViewContainerRef, private compiler: Compiler) {}

  ngOnChanges() {
    if(!this.compile) {
      if(this.compRef) {
        this.updateProperties();
        return;
      }
      throw Error('You forgot to provide template');
    }

    this.vcRef.clear();
    this.compRef = null;

    const component = this.createDynamicComponent(this.compile);
    const module = this.createDynamicModule(component);
    this.compiler.compileModuleAndAllComponentsAsync(module)
      .then((moduleWithFactories: ModuleWithComponentFactories<any>) => {
        let compFactory = moduleWithFactories.componentFactories.find(x => x.componentType === component);

        this.compRef = this.vcRef.createComponent(compFactory);
        this.updateProperties();
      })
      .catch(error => {
        console.log(error);
      });
  }

  updateProperties() {
    for(var prop in this.compileContext) {
      this.compRef.instance[prop] = this.compileContext[prop];
    }
  }

  private createDynamicComponent (template:string) {
    @Component({
      selector: 'custom-dynamic-component',
      template: template,
    })
    class CustomDynamicComponent {}
    return CustomDynamicComponent;
  }

  private createDynamicModule (component: Type<any>) {
    @NgModule({
      // You might need other modules, providers, etc...
      // Note that whatever components you want to be able
      // to render dynamically must be known to this module
      imports: [CommonModule],
      declarations: [component]
    })
    class DynamicModule {}
    return DynamicModule;
  }
}
Run Code Online (Sandbox Code Playgroud)

用法:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
})
export class AppComponent {

  name = 'Product Name :'
  price = '3'
  template: string = `{{ name }} <b>{{ price }}$</b>`;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="product">
  <ng-container *compile="template; context: this"></ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)

演示:https : //stackblitz.com/edit/angular-eipbup? file = src%2Fapp% 2Fapp.component.html