我正在尝试根据item._id .i存储项目在组件中更改标题。 这是我的HTML
<h1 mat-dialog-title>{{item._id ? "Update" : "Add"}} animal</h1>
下面是我的dialog-overview-example.ts
import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
/**
* @title Dialog Overview
*/
@Component({
selector: 'dialog-overview-example',
templateUrl: 'dialog-overview-example.html',
styleUrls: ['dialog-overview-example.css'],
})
export class DialogOverviewExample {
animal: string;
name: string;
item:string;
constructor(public dialog: MatDialog) {}
openDialog(): void {
item =[{"_id": "2","animal":"lion","weiht":"100"}];
let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '250px',
data: { name: this.name, animal: this.animal,item: this.item }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
}
@Component({
selector: 'dialog-overview-example-dialog',
templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) { }
onNoClick(): void {
this.dialogRef.close();
}
}
如果项目标题中有_id应该显示更新动物,否则它应该在我们的案例ID中显示添加动物已经存在于项目中所以它应该显示更新动物..帮助我
答案 0 :(得分:1)
我可以看到您已将组件内的项目定义为数组。所以你必须在HTML中使用以下代码
<h1 mat-dialog-title>{{item[0]._id ? "Update" : "Add"}} animal</h1>