How to detect async change to ng-content

后端 未结 2 360
孤城傲影
孤城傲影 2020-12-11 05:37

I made a component which uses the marked package to render markdown content, the thing is it doesn\'t re-render itself when an async event changes its n

2条回答
  •  我在风中等你
    2020-12-11 05:55

    Even if the Günter's answer is great, I wasn't able to resist to create a little plunkr describing how to use Marked into a component: https://plnkr.co/edit/0oSeaIyMWoq5fAKKlJLA?p=preview.

    Here are the details:

    • Marked configuration the HTML file

      
      
    • The component that uses Marked

      import { Component, Input } from 'angular2/core';
      import marked from 'marked';
      
      @Component({
        selector: 'markdown', 
        template: `
          
      ` }) export class MarkdownComponent { @Input('data') data:string; ngOnChanges() { this.convertedData = marked(this.data); } }
    • The component that uses the previous Markdown component

      import { Component } from 'angular2/core';
      import { MarkdownComponent } from './markdown';
      
      @Component({
        selector: 'my-app', 
        template: `
          
      `, directives: [ MarkdownComponent ] }) export class AppComponent { constructor() { this.markdown = 'Hello'; setTimeout(() => { this.markdown = ` # Title Some __test__ `; }, 1000); } }

提交回复
热议问题