私は以下のID:59dc921ffedff606449abef5
を動的にMatDialog
に渡す必要があります。テストのために、私はそれをハードコードされたIDとして使用しています。動的属性/パラメータをopenDialogに渡すにはどうすればよいですか?
残念ながら、すべての検索と試行が失敗し、idを動的に関数呼び出しに取得できません。私も@input
の機能を試しましたが、それは役に立たなかった。
edit-dilog.component.ts
:
export class EditDialogComponent implements OnInit {
dialogResult:string = '';
constructor(public dialog:MatDialog, public loginService:LoginService){ }
ngOnInit() {}
openDialog() {
this.dialog.open(EditUserComponent, { data: '59dc921ffedff606449abef5' })
.afterClosed()
.subscribe(result => this.dialogResult = result);
}
}
edit-user.component.ts
:
export class EditUserComponent implements OnInit {
public message:any [];
public resData: {};
constructor(public thisDialogRef: MatDialogRef<EditUserComponent>,
@Inject(MAT_DIALOG_DATA) public data: number,
public loginService: LoginService) { }
ngOnInit() {
this.loginService.getSingleUser(this.data)
.subscribe(data => {
this.resData = JSON.stringify(data);
})
}
onCloseConfirm() {
this.thisDialogRef.close('Confirm');
}
onCloseCancel() {
this.thisDialogRef.close('Cancel');
}
}
IDがサービスでJSON応答から来ているログイン-service.ts:
getSingleUser(id) {
return this.http.get(environment.urlSingleUsers + '/' + id, this.options)
.map(res => {
console.log('RES: ' + JSON.stringify(res.json()));
return res.json();
}).catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
extractData(result:Response):DialogUserData[] {
return result.json().message.map(issue => {
return {
ID: issue._id,
Email: issue.email,
Name: issue.fullName
}
});
}
そして、ここで私がopenDialog()
の呼び出し行う場所である:私はちょうどかかわらず、類似した何かをした
"message": [
{
"_id": "59415f148911240fc812d393",
"email": "[email protected]",
"fullName": "Jane Doe",
"__v": 0,
"created": "2017-06-14T16:06:44.457Z"
},
{
"_id": "5943b80be8b8b605686a67fb",
"email": "[email protected]",
"fullName": "John Doe",
"__v": 0,
"created": "2017-06-16T10:50:51.180Z"
}
]