这是代码:
app.component.html
<input type="text" [(ngModel)]="newAreaName"/><button (click)="addItem()" [disabled]="editing ? false : true">ADD</button>
<button (click)="toggleEditing()">{{ editing ? 'Cancel' : 'Edit'}}</button>
<app-line (layout)="setFloorLayout($event)" [editing]="editing" [newAreaName]="newArea"></app-line>
app.component.ts
flrLayout = [];
editing = true;
newAreaName = "";
newArea = "";
setFloorLayout(data: any) {
this.flrLayout = data;
}
addItem(): void {
this.newArea = this.newAreaName;
this.newAreaName = "";
console.log(`SUCCESS ADDING`);
}
toggleEditing() {
this.editing = !this.editing;
}
子组件 line.component.ts
@Input() newAreaName: string;
@Output() layout: EventEmitter<any> = new EventEmitter();
@Input() editing: any;
options: Safe;
dashboard = [];
ngOnInit() {}
ngOnChanges() {
console.log(this.dashboard);
if (this.newAreaName && this.editing) {
this.addItem(this.newAreaName);
}
}
addItem(areaName: string): void {
this.dashboard.push({ x: 0, y: 0, cols: 1, rows: 1, area: areaName });
this.layout.emit(this.dashboard);
}
这里的问题是在添加新数据之后,当我单击取消然后当我尝试单击编辑按钮时,它会自动附加新数据。相反,仪表板数组将为空。
这是代码和输出:https ://stackblitz.com/edit/angular-rc7bbz?file=src%2Fapp%2Fapp.component.ts
注意:添加数据后,单击取消然后单击编辑按钮。
假设有一个现有数据
dashboard = [{
area: "Area1"
cols: 1
rows: 1
x: 0
y: 0
}]
然后添加一个“Area2”数据应该是这样的。
dashboard = [{
area: "Area1"
cols: 1
rows: 1
x: 0
y: 0
},{
area: "Area2"
cols: 1
rows: 1
x: 0
y: 0
}]
然后当它单击取消按钮并单击编辑按钮时,它应该是这样的:
dashboard = [{
area: "Area1"
cols: 1
rows: 1
x: 0
y: 0
}]