【问题标题】:Transcluding a component several times in Angular?在 Angular 中多次嵌入一个组件?
【发布时间】:2019-07-13 06:36:48
【问题描述】:

我想实现以下目标:

<s-panel-edicion>
  <s-barra-lateral posicion="right" icono="fa fa-cog" [animar]="true">cualquier contenido</s-barra-lateral>
  <s-barra-lateral posicion="left" icono="fa fa-cog" [animar]="true">cualquier contenido </s-barra-lateral>
 </s-panel-edicion>

左右是侧边栏的位置,这是我的编辑组件:

<div class="container-plantilla">
  <div class="plantilla-template" [ngStyle]="moverBarraLeft">
    <ng-content select="s-barra-lateral"></ng-content>
  </div>
  <div class="plantilla-contenido" [ngClass]="{'plantilla-contenido-oculto':!displayLeft,'plantilla-contenido-no-oculto':displayLeft,'plantilla-contenido-full':displayLeft}">
    <ng-content select="s-contenido"></ng-content>
  </div>
  <div class="plantilla-template" [ngStyle]="moverBarraRight">
    <ng-content select="s-barra-lateral"></ng-content>
  </div>
</div>

只显示一侧。有人可以指导我如何实现这一目标吗?感谢您的帮助。

(我的英文不好,谢谢)

【问题讨论】:

    标签: angular transclusion


    【解决方案1】:

    如果我正确理解您的要求,您需要按以下方式使用“选择”:
    select="[name]",然后您可以在任何标签中使用“名称”。

    您的 html 将如下所示:

    <s-panel-edicion>
      <s-barra-lateral content1 posicion="right" icono="fa fa-cog" [animar]="true"> cualquier contenido</s-barra-lateral>
      <s-barra-lateral content3 posicion="left" icono="fa fa-cog" [animar]="true">cualquier contenido </s-barra-lateral>
    </s-panel-edicion>
    

    s-panel-edicion:

    <div class="container-plantilla">
      <div class="plantilla-template" [ngStyle]="moverBarraLeft">
        <ng-content select="[content1]"></ng-content>
      </div>
      <div class="plantilla-contenido" [ngClass]="{'plantilla-contenido-oculto':!displayLeft,'plantilla-contenido-no-oculto':displayLeft,'plantilla-contenido-full':displayLeft}">
        <ng-content select="[content2]"></ng-content>
      </div>
      <div class="plantilla-template" [ngStyle]="moverBarraRight">
         <ng-content select="[content3]"></ng-content>
      </div>
    </div>
    


    循环示例

    <s-panel-edicion>
      <div *ngFor="let item of items">
        <s-barra-lateral *ngIf="item.posicion == 'right'" content1 posicion="right" icono="fa fa-cog" [animar]="true"> cualquier contenido</s-barra-lateral>
        <s-barra-lateral *ngIf="item.posicion == 'left'" content3 posicion="left" icono="fa fa-cog" [animar]="true"> cualquier contenido</s-barra-lateral>
      </div>   
    </s-panel-edicion>
    

    【讨论】:

    • 我想避免这种使用,我正在寻找一种解决方案,我可以添加更多 并能够在 ng-content 中使用 for 来管理它,以便它是根据位置动态的吗?
    • 您能展示一下您希望它如何工作吗?您能否使用 ngFor 展示您的代码以及您希望它如何展示?
    • 我不知道,但我看到了类似的东西我不知道如何应用它
    • 我添加到答案中的循环示例怎么样?是你要求的吗?
    • 不,我需要例如 PRIMENG

      即使如此投票给你的答案,但如果我取得了更先进的东西,那就太好了
    猜你喜欢
    • 2020-05-26
    • 1970-01-01
    • 2019-03-03
    • 2018-07-14
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多