javascript - 将 ng-content 传递给子组件

标签 javascript html angular typescript angular-material2

谢谢你的时间。

我有三个组件:

  • 主页组件
  • 使用angular/cdk/overlay的按钮overlay.create创建第三个组件
  • 附在显示文本的按钮上的小框

  • 目标是在用户单击按钮时显示有关页面的信息。我无法使用 @Input ,导致每个页面的信息格式(h1p、组件)都会发生变化。

    我的问题

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

    或者

    如何截获 ng-content 的内容并将其发送到小盒子组件?

    主要的

    <app-btn-info>
       <mat-icon>info</mat-icon>
       <h1>Test</h1>
       <p>This is a test</p>
    </app-btn-info>
    

    按钮

    @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' }
          )
        });
      }
    
    }
    

    <button #button mat-icon-button color="warn" matTooltip="Choose an option" (click)="onClick()">
        <mat-icon>refresh</mat-icon>
    </button>
    

    小盒子

    <!-- From Main Component - But open by BtnInfoComponent -->
    <ng-content></ng-content> 
    

    最佳答案

    如果有人来这里,以供将来引用,这可能会有所帮助-
    https://stackblitz.com/edit/angular-ivy-apjclv?file=src/app/child/child.component.html
    编辑:
    基本上,您在容器中寻找的内容:

    <app-parent>
    <span> some content from container </span>
    </app-parent>
    
    在 parent 中:
    <app-child>
      <ng-content select="[foo]" ngProjectAs="[foo]"> </ng-content>
    </app-child>
    
    在 child 身上:
    <ng-content select="[foo]"> </ng-content>
    
    可以在父级中添加内容,但您需要将其拆分为:
    <ng-container ngProjectAs="[foo]">
      <span>some parent content</span>
      <ng-content select="[foo]"></ng-content>
    <ng-container>
    

    编辑 2:所以我对传递 TemplateRefs 感到厌烦,以便能够从投影内容的组件(在这些示例中也称为“父级”)访问变量。然而,访问比“父级”更深的变量是站不住脚的。如果您需要访问子变量,那么我建议您改用服务/可观察/重新考虑您的结构。
    TemplateRef 示例 ,使用上面相同的容器/父/子结构:
    // parent-foo.component.ts
    @Input() templateFooSelector?: TemplateRef<any>;
    
    <!-- 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>
    
    <!-- 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>
    

    关于javascript - 将 ng-content 传递给子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028401/

    相关文章:

    javascript - 谷歌地图中的组标记

    javascript - 将禁用样式 (css) 添加到输入类型文件按钮

    javascript - 如何检测/阻止用户打开新的浏览器窗口?

    jquery - Angular 6 引用错误 : $ is not defined error with JQuery

    angular - 如何使用 Angular [innerHTML] 修复 TrustedHTML 分配错误

    javascript - react ,改变状态只适用于一个功能

    javascript - 发送表单时向表单插入新元素会导致问题(空数据)

    html - 如何使用 CSS 更改文本?

    javascript - JQuery 和 CSS 屏幕键盘无法在重制 HTML 上运行

    angular - Jasmine Angular : How to write unit test for anonymous func given as args