【发布时间】:2020-06-18 21:10:09
【问题描述】:
我有打开模式的父组件。 (子组件)。
parent.ts 文件:-
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss']
})
export class ParentComponent implements OnInit, OnChanges {
faCheck = faCheck;
verticalArr = ['course completed',
'attendance',
'new attendance',
'trainer',
'view'];
horizontalArr = ['time', 'city'];
constructor(private reportService: ReportService, private router: Router, private dialog: MatDialog) {
}
openDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
this.dialog.open(XmodalComponent, {
height: '50%', width: '100%',
data: this.horizontalArr
});
}
openDialog2() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
this.dialog.open(YmodalComponent, {
height: '50%', width: '100%',
data: this.verticalArr
});
}
}
子组件(模态):-
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { faCheck } from '@fortawesome/free-solid-svg-icons';
@Component({
selector: 'app-xmodal',
templateUrl: './xmodal.component.html',
styleUrls: ['./xmodal.component.scss']
})
export class XmodalComponent implements OnInit {
faCheck = faCheck;
selectedItems = [];
selectedHorizontalValue: string;
constructor(public dialogRef: MatDialogRef<XmodalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) { }
xarray = this.data;
ngOnInit() {
// will log the entire data object
console.log(this.xarray);
}
onHorizontalAxisSelect(key) {
this.selectedItems = [];
this.selectedHorizontalValue = key;
}
getSelectedHorizontalAxis() {
console.log(this.selectedHorizontalValue); //<=== (select from button click either time or city
return this.selectedHorizontalValue;
}
}
子 html(模态):-
选择水平轴
<div class="axis-selection">
<div class="menu">
<ng-container *ngFor="let key of xarray">
<button (click)="onHorizontalAxisSelect(key)"
[ngClass]="{ 'selected': key === getSelectedHorizontalAxis() }">{{key}}
<fa-icon *ngIf=" key === getSelectedHorizontalAxis() " [icon]="faCheck"></fa-icon></button>
</ng-container>
</div>
</div>
(与 ycomponent 模态相同)
因此,子组件中的this.selectedHorizontalValue 包含选定的值。如何将此值传递给父组件并将其存储在新变量中或使用相同的变量名存储,即; this.selectedHorizontalValue??
对不起,我是打字稿学习的新手。
【问题讨论】:
-
@Yeheshuah 有点理解,但不知道在我的情况下发射器是什么。我的意思是
-
我的错。显然,this answer 更适用于您的问题。
-
@Yeheshuah 抱歉,但我无法理解发出服务。你能根据我的代码解释一下吗?
-
看一下 Material 示例,数据从对话框发送回组件 material.angular.io/components/dialog/examples 对话框 html 中的按钮可以具有类似 [mat-dialog-close]="key" 的值
标签: angular typescript