首页 > 解决方案 > 使用具有选择器值的变量作为字符串Angular呈现html内容

问题描述

我从上周开始研究 Angular。我正在尝试不直接使用其选择器来呈现组件,我正在将该选择器值作为某个变量中的字符串,并且我试图在 html 文件中传递该变量,但它正在将其视为字符串值而不是 html 标记。

这是我的 ts 文件,其中选择器是app-out

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-out',
  templateUrl: './out.component.html',
  styleUrls: ['./out.component.scss']
})
export class OutComponent implements OnInit {
  my_comp;
  constructor(_TestComponent: TestComponent) {
    this.my_comp = _TestComponent;
  }

  ngOnInit() {
    console.log("hskjfhk", this.my_comp.impo)
  }

}

在这里,我取了变量mySelVar的值,它的值与我的选择器相同。

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'primeng-sandbox';
  mySelVar = 'app-out';

  
public imageObject;
  ngOnInit(){
    
  }
}

这是html,我想使用该变量呈现该组件,但我知道为什么它不将其视为选择器

<courses></courses>
<app-test></app-test>
<{{mySelVar}}>

如果我这样做,它就可以工作, <app-out></app-out>但我想改用变量。请帮助我!

标签: htmlangulartypescript

解决方案


推荐阅读