node.js - Angular 6 在同一文件中的组件之间使用传递变量
问题描述
我有一个使用 API 工作的 Angular 6 应用程序。在我的应用程序中,我必须在 Angular Material 对话框中显示一些传入数据,因此对话框组件与我用来显示 API 数据的主组件位于旁边。
事情是这样的,我想将我的一些数据传输到 Dialog 组件,例如:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-jours-feries',
templateUrl: './show-data.component.html',
styleUrls: ['./show-data.component.scss']
})
export class ShowDataComponent {
public data: any;
// data here will get the incoming data from the API
}
@Component({
selector: 'dialog.component',
templateUrl: 'dialog.component.html',
styleUrls: ['./dialog.component.scss']
})
export class DialogComponent {
// I want to use data property in this component without duplicating functions and variables
}
这是我的原始代码:
import { Component, OnInit, Inject } from '@angular/core';
import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms';
import { ErrorStateMatcher } from '@angular/material/core';
import { MatDialog, MAT_DIALOG_DATA } from '@angular/material';
import { ConfigurationService } from 'src/app/services/configuration.service';
export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isSubmitted = form && form.submitted;
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
}
}
@Component({
selector: 'app-jours-feries',
templateUrl: './jours-feries.component.html',
styleUrls: ['./jours-feries.component.scss']
})
export class JoursFeriesComponent implements OnInit {
public displayedColumns: string[] = ['date', 'reserve', 'description', 'actions'];
public jourFeriesObjectHolder: any;
public description: any;
constructor(
private dialog: MatDialog,
private __ConfigurationService: ConfigurationService
) {
this.getJoursFeries();
}
/**
* Getting {{ Congés Annuels }} data from our service
*
*/
public getJoursFeries(){
this.__ConfigurationService.getJoursFeries().subscribe((data) => {
this.jourFeriesObjectHolder = data;
});
}
public getJoursFeriesDescription(){
this.__ConfigurationService.getJoursFeriesDesc().subscribe((data) => {
// this.jourFeries_IDHolder = data[0]._id;
// this.descExplicatifJourFer = data[0].description;
this.description = data[0].description;
});
}
openDialog() {
const dialogRef = this.dialog.open(AddUpdateJourFerieComponent, { data: this.description });
dialogRef.afterClosed().subscribe(confirm => {
if(confirm){
// do something
}
});
}
ngOnInit() {
}
}
@Component({
selector: 'add-update.component',
templateUrl: 'add-update.component.html',
styleUrls: ['./add-update.component.scss']
})
export class AddUpdateJourFerieComponent {
// Validate Jours Fériés
requiredDescriptionJoursFeries = new FormControl('', [
Validators.required
]);
requiredNameJoursFeries = new FormControl('', [
Validators.required
]);
requiredDateJoursFeries = new FormControl('', [
Validators.required
]);
requiredTextJoursFeries = new FormControl('', [
Validators.required
]);
matcher = new MyErrorStateMatcher();
/* Jour ferié */
public jourFerDate: string;
public isReservedHoliday: number = 0;
public descJourFer: string;
public descExplicatifJourFer: string;
public jourFeries_IDHolder: string;
constructor(@Inject(MAT_DIALOG_DATA) public data: any) {
console.log(data);
}
}
任何想法 ?
解决方案
let dialogRef = dialog.open(DialogComponent, {Data: { }})
角度材料对话框的参考链接
推荐阅读
- python - Tkinter get() 无法返回用户输入
- windows - 为什么 WriteConsoleW 在使用 ml64 调用 CoInitialize 后会中断
- c# - OpenApi 生成的 c# 代码中的不明确引用错误
- php - 动态文件介绍 SQL 查询
- javascript - 使用文件类型的缓冲区中的文件类型
- java - Android Room Java Inner Join with Count
- sql - SQL中的XML PATH是否有替代方案
- android - android studio 无法识别设备上当前安装的 apk(is)
- android - 如何在 Firebase 聊天应用中实现消息可见功能
- python - 如何仅从 excel 文件中过滤出那些具有我在代码中提到的特定单词的评论?