我在数据库中有一个包含以下字段的对象: isRed、isToday、isImportant --所有这些字段都是布尔值,还有一个Id。
在我的UI中,我有3个带有输入的复选框,在我的ts文件中,如何更新对象?我尝试创建一个新项目并将其发送到服务中。问题是,我希望只发送一个字段,2或3。不是所有字段都同时发送。
html代码:
<div class="mb-2 alternate-warning-checkbox-wrapper">
<mdb-checkbox [(ngModel)]="isRed" [default]="false">isRed</mdb-checkbox>
</div>
<div class="mb-2 alternate-warning-checkbox-wrapper">
<mdb-checkbox [(ngModel)]="isToday" [default]="false">isToday</mdb-checkbox>
</div>
<div class="mb-2 alternate-warning-checkbox-wrapper">
<mdb-checkbox [(ngModel)]="isImportant " [default]="false">isImportant</mdb-checkbox>
</div>
<button type="button" mdbBtn class="btn-sm" (click)="updateSupplier()" color="dark-green" mdbWavesEffect>Save</button>ts代码:
import { Component, Input, OnInit } from '@angular/core';
import { SupplierService } from '@app/_core/api/supplier.service';
@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class App implements OnInit {
@Input() isRed: boolean;
@Input() isToday: boolean;
@Input() isImportant : boolean;
constructor(
private itemService: ItemService,
) {
}
ngOnInit() {
}
updateSupplier() {
let item = [this.isRed,
this.isToday,
this.isImportant
];
this.itemService.updateItem(item)
}
}服务:
updateItem(item: Item) {
return this.apiService.put(`${this.resourceUrl}/update`, supplier);
}发布于 2020-02-03 12:12:07
您可以将Item的属性设置为nullable,并且您的服务器也应该接受它们为null。
class Item: {isRed?: boolean, isToday?: boolean, isImportant?: boolean}在ts文件中:
this.itemService.updateItem({isBoolean: this.isBoolean})然后在服务中,您可以检查所有的道具是否为空,然后不要发送任何请求。
https://stackoverflow.com/questions/60038515
复制相似问题