简体   繁体   English

Angular2 [innerHtml] angular2 标签不起作用

[英]Angular2 [innerHtml] angular2 tag doesn't work

I want to inject html in my a-component with angular2 tags from another component.我想用来自另一个组件的 angular2 标签在我的 a 组件中注入 html。

@Component({
  selector: 'app-root',
  template: '<app-a [my-html]="my-html"> </app-a>',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  my-html= '<span> {{variableFromAComponent}}</span>';
}



@Component({
      selector: 'app-a',
      template: '<div [innerHtml]="my-html"> </div>',
    })
    export class AComponent {
      @Input('my-html') my-html:any;
      public variableFromAComponent='its work!'; 
    }

I want to see in result : its work!我想看到结果:它的工作! (from variableFromAComponent) But i see {{variableFromAComponent}} (angular2 tags doesn't work). (来自 variableFromAComponent)但我看到 {{variableFromAComponent}} (angular2 标签不起作用)。

I found a solution in angular2-dynamic-component我在angular2-dynamic-component 中找到了解决方案

<template dynamic-component
          [componentContext]="self"
          [componentModules]="dynamicExtraModules"
          [componentTemplate]='"here html tags with angular2 tags"'>
</template>

Angular doesn't process HTML added by [innerHTML]="..." at all. Angular 根本不处理由[innerHTML]="..."添加的 HTML。 It is just added as is and that's it.它只是按原样添加,仅此而已。 You might even need to use the Sanitizer, so nothing gets stripped for security reasons (see In RC.1 some styles can't be added using binding syntax ).您甚至可能需要使用 Sanitizer,因此出于安全原因,不会删除任何内容(请参阅在 RC.1 中,某些样式无法使用绑定语法添加)。

If you want to dynamically add components, you can use ViewContainerRef.createComponent() for example like explained in Angular 2 dynamic tabs with user-click chosen components如果你想动态添加组件,你可以使用ViewContainerRef.createComponent()例如像Angular 2 动态选项卡中解释的那样,用户单击选择的组件

There are a couple ways that you can do this.有几种方法可以做到这一点。

COMPONENT INTERACTION 组件交互

If there is a parent/child relationship between the components then you can use Input() and Output() to pass values between them如果组件之间存在父/子关系,则可以使用Input()Output()在它们之间传递值

This child component gets the value through the Input()这个子组件通过Input()获取值

child.component.ts子组件.ts

import { Component, Input } from '@angular/core';
@Component({
  selector: 'child',
  template: '<div>{{myHtml}}</div>', // these curly braces add the text as innerHTML
  providers: [ FoodsService]
})
export class ChildComponent implements OnInit {
   @Input() myHtml: string;
}

which is passed from the parent through the template:这是从父级通过模板传递的:

parent.component.html父组件.html

<child [myHtml]="'You're String Here (or a variable set to "its work")'"></child>

You can use Output() to go from child to parent.您可以使用Output()从孩子到父母。

SERVICES服务

The other way is to create a service that gets injected into both components.另一种方法是创建一个注入到两个组件中的服务。 One component can send a value to the service and the other can get that value.一个组件可以向服务发送一个值,另一个可以获取该值。 In Angular2 this is often achieved by using observables to a data object.在 Angular2 中,这通常是通过对数据对象使用observable来实现的。

You can use Renderer2 Class for this.您可以为此使用Renderer2 类

import { ElementRef, Renderer2 } from '@angular/core';

constructor (
  private elementRef: ElementRef,
  private renderer: Renderer
) {}

public ngAfertViewInit(): void {
  this.renderer
     .setElementProperty(
        this.elementRef.nativeElement, 
        'innerHTML',
        '<h1> Nice Title </h1>'
     );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM