写项目的时候遇到一个需求:a.html中内容有两个状态,当放在edit.html中的时候,a.html是input框,当在show.html中使用a.html的时候,a.html是label。

angular7+ 灵活组件 使用@input使同一组件在不同模板(template)显示不同的内容

结构如图,应该是一个很简单的功能,但是因为对父子传值概念不清晰,@input使用不熟练,导致这个问题卡了一天,最后解决代码如下

代码:

a.html:

<div class="input">

     <input pInputText placeholder="请输入单位名称" formControlName="orgName" *ngIf="formStatu == 'edit'">

      <label *ngIf="formStatu == 'show'">{{basicInfoForm.value.orgName}}</label>

</div>

 

a.ts:

@Input() formStatu

 

b.html:

 <app-org-basic-mess [formStatu]="edit"></app-org-basic-mess>

 <app-check-info></app-check-info>

 

b.ts:

edit = 'edit'

 

c.html:

<app-org-basic-mess [formStatu]='show' #basic></app-org-basic-mess>

 

c.ts:

show = 'show'

努力想把逻辑给大家写的简单,所以只把主要的代码粘出来,其中组件a是文中的org-basic-mess,是最底层的子组件,组件b是文中的是顶层父组件,组件c是文中的check-info,他既是组件b的子组件,又是组件a的父组件

相关文章:

  • 2021-12-26
  • 2021-10-28
  • 2021-12-31
  • 2022-02-06
  • 2022-12-23
  • 2021-11-03
  • 2021-08-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2021-09-08
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案