简体   繁体   English

html不会从angular 2组件内部进行渲染

[英]html wont render from inside angular 2 component

I'm using a service to dynamically change the content in my header depending on the page I'm on, however when I put HTML in my component it doesn't render in the browser (see example below) 我正在使用服务来根据我所在的页面动态更改标题中的内容,但是当我将HTML放入组件时,它不会在浏览器中呈现(请参见下面的示例)

home.component.ts home.component.ts

import { Component, OnInit } from '@angular/core';
import { HeaderTitleService } from '../../services/headerTitle.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  constructor(
    private headerTitleService: HeaderTitleService
  ) { }

  ngOnInit() {
    this.headerTitleService.setTitle(`
      We strive to create things
      <br> that are engaging, progressive
      <br> &amp; above all
      <span class="highlight">
      <em>innovative.</em>
      </span>
    `);
  }

}

header.component.ts header.component.ts

import { Component, OnInit } from '@angular/core';
import { HeaderTitleService } from '../../../services/headerTitle.service'

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
  title: any;

  constructor(
    private headerTitleService: HeaderTitleService
  ) { }

  ngOnInit() {
    this.headerTitleService.title.subscribe(updatedTitle => {
      this.title = updatedTitle;
    });
  }

}

header.component.html header.component.html

<h1>{{title}}</h1>

so Im trying to set the title to be a string that has html tags in it that I want to be rendered but what happens is the whole thing comes out as a string instead of how it would look like it I had put it in my home.component.html. 所以我试图将标题设置为一个我想要呈现的带有html标签的字符串,但是发生的是整个事情以字符串形式出现,而不是我放在家里的样子.component.html。

Is there a way I can do this?? 有什么办法可以做到吗?

You can set the [innerHtml] property 您可以设置[innerHtml]属性

<h1 [innerHtml]="title"></h1>

Example

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

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