我怎样才能对同一打字稿文件使用HTML模板的差异?

时间:2019-04-22 17:13:17

标签: angular typescript

我需要对同一打字稿文件使用不同的html模板,该模板基于构造函数中的表达式。这有可能吗?

我需要类似的东西:

<div class="container-fluid">
     <app-teste1 *ngIf="teste == '1'>
     <app-teste2 *ngIf="teste == '2'>
</div>

构造函数中

this.teste = 1

因此,必须渲染第一个组件(app-teste1)。

但是我的组件具有很多属性,还有另一种方法可以在不传递各种输入/输出属性的情况下做到这一点?

2 个答案:

答案 0 :(得分:0)

我认为该功能现在不可用(angular 8.0.0-beta.13)。

您可以用许多不同的方式来操作和渲染DOM树,但到目前为止,无法以角度加载不同的模板(.html)文件。

答案 1 :(得分:0)

您可以从基类继承并使用其他模板。像这样:

@Component({
  selector: 'app-teste1',
  template: `<div>this is app-teste1</div>`
})
export class TestOneComponent {
    // ...
}

@Component({
  selector: 'app-teste2',
  template: `<div>this is app-teste2</div>`
})
export class TestTwoComponent extends TestOneComponent {
    // ...
}