Expand/Collapse 组件之间的功能

Expand/Collapse functionality between components

我使用了 2 个不同的组件 Angular - 用于扩展摘要视图的扩展面板。

我能够将 <mat-expansion-panel> 成功地集成到各个组件中。

我需要帮助让 Component2.html 成为 Component1.html 的父级(在展开中展开 - 请参见下图)

注意 - 两个组件都有 兄弟关系no parent childchild - parent

Component1.html

  <div class="row">
    <div class>
      <dl class="row property_set" data-property-set-name="data">
        <mat-accordion>
          <mat-expansion-panel (opened)="doExpand = true"
                             (closed)="doExpand = false">
          <mat-expansion-panel-header>
            <mat-panel-title>
        <dt class="col-sm-4 record_property">data</dt>
      </mat-panel-title>
      <mat-panel-description>
        <dd class="col-sm-8 record_property_value data_name" id="{{genId(data.name)}}">
          <inline-misal-edit 
                        [(field)]="data.name" [elementType]="a.bName" (fieldChange)="dataModified(data)"
                        cols="30" rows="1"></inline-misal-edit>
          </dd>
      </mat-panel-description>
    </mat-expansion-panel-header>

        <dt class="col-sm-4 record_property">news</dt>
        <dd class="col-sm-8 record_property_value">{{data.news?.created | news}}</dd>
      </mat-expansion-panel>
    </mat-accordion>
      </dl>
    </div>

Component2.html


  <dl class="row property_set" data-property-set-name="misal">
    <mat-accordion>
        <mat-expansion-panel (opened)="doExpand = true"
                             (closed)="doExpand = false">
          <mat-expansion-panel-header>
            <mat-panel-title>
              misal Id
            </mat-panel-title>
            <mat-panel-description>
              <dd class="col-sm-10 record_property_value" data-property-type="select">{{misal.id || 'new'}}</dd>
            </mat-panel-description>
          </mat-expansion-panel-header>
          <dd class="col-sm-10 record_property_value misal_name">{{misal.data[0].name}}</dd>
          <dt class="col-sm-2 record_property">Country Pass</dt>
          <dt class="col-sm-2 record_property">Plugins Program</dt>
          <dd class="col-sm-10 record_property_value">
            <north-dhamma[(misal)]="misal" [editMode]="editMode" (misalChange)="recordModified.emit()"></registry-number>
          </dd>
          <dt *ngIf="misal.value === 'hovered'" class="col-sm-2 record_property">Related Plugins Program</dt>
          <dd *ngIf="misal.value === 'hovered'" class="col-sm-10 record_property_value">
            <land-hole></land-hole>
          </dd>
                </mat-expansion-panel>
      </mat-accordion>
  </dl>

.ts 文件

  panelOpenState = true;                 

更新

Robbie 在下面给出的答案适用于 parent - child component relation 但不适用于 sibling component

您应该可以在 mat-expansion-panel-header

的关闭标记之后立即添加您的子组件
<mat-accordion>
  <mat-expansion-panel>
    <mat-expansion-panel-header>
      <mat-panel-title>
        Parent Expansion
      </mat-panel-title>
    </mat-expansion-panel-header>
    <p>Content of parent</p>
    <!-- add child here -->
    <app-child></app-child>
  </mat-expansion-panel>
</mat-accordion>

https://stackblitz.com/edit/angular-5euuwx?file=src%2Fapp%2Fexpansion-overview-example.html

一种方法是使用共享服务 - https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

但是我发现下面的解决方案更简单,它允许在 2 个兄弟姐妹之间共享数据。

app-sibling2.component.ts

import { AppSibling1Component } from '../app-sibling1/app-sibling1.component';

export class AppSibling2Component {
   @Input() data: AppSibling1Component;
   ...
}

在你的父组件模板中:

<!-- Assigns "AppSibling1Component" instance to variable "data" -->
<app-sibling1 #data></app-sibling1>
<!-- Passes the variable "data" to AppSibling2Component instance -->
<app-sibling2 [data]="data"></app-sibling2> 

我相信这就是您要找的。如果您有任何问题,请告诉我

我有点困惑,你想让 Component2.html 作为 Component1.html 的父级,你提到这两个组件都有兄弟关系,no parent child or child - parent

牢记这一点,如果 ExpansionWrapperComponent 包含 Component1Component2 并包含行为 - 如果 Component2 被切换如何Component1 必须同时切换 Component1 也可以单独切换.

扩展-wrapper.component.html

<div class="expansion-wrapper">
  <div class="parent">
    <ng-content select="app-component2"></ng-content>
  </div>
  <div class="child">
    <ng-content select="app-component1"></ng-content>
  </div>
</div>

扩展-wrapper.component.ts

import { AfterContentInit, Component, ContentChild, OnInit } from '@angular/core';
import { Component1Component } from '../component1/component1.component';
import { Component2Component } from '../component2/component2.component';

@Component({
  selector: 'app-expansion-wrapper',
  templateUrl: './expansion-wrapper.component.html',
  styleUrls: ['./expansion-wrapper.component.css']
})
export class ExpansionWrapperComponent implements OnInit, AfterContentInit {

  @ContentChild(Component2Component, { static: false }) component2: Component2Component;
  
  @ContentChild(Component1Component, { static: false }) component1: Component1Component;

  constructor() { }

  ngOnInit() {
  }

  ngAfterContentInit() {
    this.component2.panelStateChanged.subscribe(res => {
      this.component1.togglePanelState(res);
    });
  }

}

其中 Component1Component2 包含单独的扩展面板。 Component1Component2 的兄弟,感觉就像嵌入到 Component2.

检查申请 stackblitz

我不知道你的需求是什么。

Component 2 should embed Component 1 within itself

你需要的是这样的

// component2.component.ts
@Component({
  selector: "component2",
  template: `
    <component1></component1>
  `,
  styles: []
})
export class Component2 {
}

Both the component has Sibling relation, no parent child or child - parent

但是在这里,你想要这个。

// app.component.ts
@Component({
  selector: "app",
  template: `
    <component2></component2>
    <component1></component1>
  `,
  styles: []
})
export class AppComponent {
}

我猜你想要的是这样的,demo。 2 sibling <mat-expansion-panel> 作为一个显示单元。