angular - 如何将数据从 MatBottomSheet 返回到其父组件?

标签 angular angular-material

我有一个带有输入字段的底部表格和两个按钮提交和取消。

父组件 HTML

<div class="something" (click)="openBottomSheet()"></div>

父组件 TS
import { MatBottomSheet } from "@angular/material";


constructor(private bottomsheet: MatBottomSheet) { }

openBottomSheet(): void {
   this.bottomsheet.open(BottomsheetComponent);
}

BottomSheet 组件 HTML
<mat-form-field>
  <input type="password" matInput placeholder="Password" [formControl]="password">
</mat-form-field>
<button mat-button  (click)="closeBottomSheet()">Cancel</button>
<button mat-button  (click)="checkPassword()">Confirm Password</button>

BottomSheet 组件 TS
import { MatBottomSheetRef } from "@angular/material";

constructor(private bottomsheet: MatBottomSheetRef<BottomsheetComponent>){}

closeBottomSheet(){
  this.bottomsheet.dismiss();
}

checkPassword(){
  //Here I need to pass data to parent component
}

在输入字段中,用户将输入数据,单击提交时将完成网络调用,如果它返回响应 200,那么我需要将一个 bool 值从底部表返回到其父组件。\

我怎样才能实现上述目标?

我会像使用组件一样使用 @Output() 来做到这一点,如果是,如何在底部表中使用它?

最佳答案

您可以使用底部工作表引用及其提供的可观察对象。我使用了内联注释来获取更多详细信息。

您可以在此处找到有关底片的更多详细信息- https://material.angular.io/components/bottom-sheet/overview

// Parent component
import { MatBottomSheet } from "@angular/material";


constructor(private bottomsheet: MatBottomSheet) { }

openBottomSheet(): void {
// Take refernce of bottom sheet
   const bottomSheetRef = this.bottomsheet.open(BottomsheetComponent);
   
   // subscribe to observable that emit event when bottom sheet closes
   bottomSheetRef.afterDismissed().subscribe((dataFromChild) => {
  console.log(dataFromChild);
});
}

// Child component
import { MatBottomSheetRef } from "@angular/material";

constructor(private bottomsheet: MatBottomSheetRef<BottomsheetComponent>){}

closeBottomSheet(){
  //  pass the data to parent when bottom sheet closes.
  this.bottomsheet.dismiss(data);
}

checkPassword(){
  //Here I need to pass data to parent component
}

关于angular - 如何将数据从 MatBottomSheet 返回到其父组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60359019/

相关文章:

angular - 找不到命名空间 'Microsoft'

angular - Angular 7 和 CAS 身份验证的提示?

angular - 仅将 Angular Material 主题应用于特定组件

angular - 如何检测 Angular Material 选项卡内的滚动事件以及如何以编程方式滚动回顶部?

html - 为什么即使导入依赖项后 mat-form-field 也不起作用?

Angular Apollo 错误处理

Angular Mat Select Multiple selectionchange 查找更改了哪个选项

Angular 4 错误 "Expected 2-3 arguments, but got 1"

javascript - Angularjs 日期时间选择器时区

javascript - Angular Material md-datepicker 和 md-select 验证