小编Emm*_*son的帖子

带有字符串变量的 Angular 2 NgTemplateOutlet。

我想在 .ts 文件中有一个模板字符串并将其添加到 NgTemplateOutlet。

我希望这会奏效,但没有。

<template [ngTemplateOutlet]="template" [ngOutletContext]="$implicit">
Run Code Online (Sandbox Code Playgroud)

其中模板是范围内的字符串变量。所以我实现了这个,但是这也不能像我想要的那样工作。

import { Component, AfterViewInit } from '@angular/core';
import { ToastController } from 'ionic-angular';
import { ModalController } from 'ionic-angular';
import { DomSanitizer, SafeHtml} from '@angular/platform-browser';

@Component({
 selector: 'dynamic-report',
 templateUrl: 'dynamicReport.html',
})
export class DynamicReport implements AfterViewInit{
 private _template: string;
 context: any;

 public get template() : SafeHtml {
   return this.sanitizer.bypassSecurityTrustHtml(this._template);
 }

 constructor(public toastCtrl: ToastController, public modalCtrl:ModalController, private sanitizer: DomSanitizer) {
   this._template = "<button (click)='testFunction1('2')'>Click here 2!</button>";
   this.context = this;

 }

  testFunction1(message){
    console.log(message); …
Run Code Online (Sandbox Code Playgroud)

angular

4
推荐指数
1
解决办法
6969
查看次数

标签 统计

angular ×1