将 ng-content 传递给子组件

The*_*224 9 html javascript typescript angular-material2 angular

谢谢你的时间。

我有三个组成部分:

  1. 主页面组件
  2. 一个按钮谁使用 angular/cdk/overlay overlay.create用于创建第三个组件的
  3. 附在显示文本的按钮上的小框

目标是在用户单击按钮时显示有关页面的信息。我不能使用@Input,因为格式 ( h1,p信息,分量)将为每个页面更改。

我的问题

如何将 HTML 从主组件传递到小框组件?

或者

我怎样才能截取内容 ng-content并将其发送到小盒子组件?

主要的

<app-btn-info>
   <mat-icon>info</mat-icon>
   <h1>Test</h1>
   <p>This is a test</p>
</app-btn-info>
Run Code Online (Sandbox Code Playgroud)

按钮

@Component({
  selector: 'app-btn-info',
  templateUrl: './btn-info.component.html',
  styleUrls: ['./btn-info.component.scss']
})
export class BtnInfoComponent {

  @ViewChild('button') button: MatIcon;

  constructor(private overlay: Overlay) { }

  public onClick() {
    this.overlay.create({
      positionStrategy: this.overlay.position().connectedTo(this.button._elementRef,
        { originX: 'start', originY: 'top' }, { overlayX: 'start', overlayY: 'top' }
      )
    });
  }

}
Run Code Online (Sandbox Code Playgroud)
<button #button mat-icon-button color="warn" matTooltip="Choose an option" (click)="onClick()">
    <mat-icon>refresh</mat-icon>
</button>
Run Code Online (Sandbox Code Playgroud)

小盒子

<!-- From Main Component - But open by BtnInfoComponent -->
<ng-content></ng-content> 
Run Code Online (Sandbox Code Playgroud)

Jos*_*lie 5

为了将来参考,如果有人到达这里,这可能会有所帮助 - https://stackblitz.com/edit/angular-ivy-apjclv?file=src/app/child/child.component.html

编辑:

基本上,您在容器中寻找的内容:

<app-parent>
<span> some content from container </span>
</app-parent>
Run Code Online (Sandbox Code Playgroud)

并在父级中:

<app-child>
  <ng-content select="[foo]" ngProjectAs="[foo]"> </ng-content>
</app-child>
Run Code Online (Sandbox Code Playgroud)

在孩子身上:

<ng-content select="[foo]"> </ng-content>
Run Code Online (Sandbox Code Playgroud)

可以在父级中添加内容,但您需要将其拆分,如下所示:

<ng-container ngProjectAs="[foo]">
  <span>some parent content</span>
  <ng-content select="[foo]"></ng-content>
<ng-container>
Run Code Online (Sandbox Code Playgroud)

编辑 2:所以我对传递 TemplateRefs 感到着迷,以便能够从投影内容的组件(在这些示例中又称为“父级”)访问变量。然而,访问比“父级”更深的变量是站不住脚的。如果您需要访问子变量,那么我建议使用服务/可观察对象/重新考虑您的结构。

TemplateRef 示例,使用上面相同的容器/父/子结构:

// parent-foo.component.ts
@Input() templateFooSelector?: TemplateRef<any>;
Run Code Online (Sandbox Code Playgroud)
<!-- container.component.html -->
<app-parent [templateFooSelector]="containerTemplate"></app-parent>

<ng-template #containerTemplate let-parentFoo="parentFoo">
  <div *ngIf="parentFoo.bar"> ParentFoo has variable Bar: {{parentFoo.bar}} </div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
<!-- parent.component.html -->
<app-child>
  <!-- this ngProjectAs is only necessary if the child uses a 'select="..." ` -->
  <ng-container ngProjectAs="[childSelector]">
    <!-- the ngIfs may be unnecessary, haven't tested without -->
    <ng-content *ngIf="!templateFooSelector" select="[fooSelector]"></ng-content>
    <ng-container *ngIf="templateFooSelector">
      <ng-container *ngTemplateOutlet="templateFooSelector; context: {parentFoo: this}">
      </ng-container>
    </ng-container>
  </ng-container>
<app-child>
Run Code Online (Sandbox Code Playgroud)