簡體   English   中英

用角度更新對象中的字段

[英]update fields in object with angular

我在數據庫中有一個對象,其中包含以下字段:isRed、isToday、isImportant——所有這些都是布爾值和一個 Id。

在我的用戶界面中,我的 ts 文件中有 3 個帶有輸入的復選框,如何更新對象? 我試圖創建一個新項目並將其發送給服務,但我希望只能發送一個字段,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);
  }

您可以將 Item 的屬性設置為可空,並且您的服務器也應將它們接受為空。

class Item: {isRed?: boolean, isToday?: boolean, isImportant?: boolean}

在您的 ts 文件中:

this.itemService.updateItem({isBoolean: this.isBoolean})

然后在服務中您可以檢查所有道具是否為空然后不發送任何請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM