【问题标题】:Sending form data as an array将表单数据作为数组发送
【发布时间】:2021-10-21 03:59:30
【问题描述】:

我有一个表单,其中一个循环有五个字段,我想将其发送到 API 以便将其保存到数据库中。

这是product.page.html

<details *ngFor="let product of products;">
    <summary>{{product.name}}</summary>
    <ion-input type="text" [(ngModel)]="categoryId" value="{{product.categoryId}}"></ion-input>
    <ion-input type="text" [(ngModel)]="productId" value="{{product.id}}"></ion-input>

    <ion-row>
        <ion-col>
            <ion-input type="text" [(ngModel)]="minPrice" value="{{product.minPrice}}"></ion-input>
        </ion-col>

        <ion-col>
            <ion-input type="text" [(ngModel)]="maxPrice" value="{{product.maxPrice}}"></ion-input>
        </ion-col>
    </ion-row>
    <ion-row>
        <ion-col>
            For Sale:
            <ion-select [(ngModel)]="forSale" interface="popover">
                <ion-select-option value="0">No</ion-select-option>
                <ion-select-option value="1">Yes</ion-select-option>
            </ion-select>
        </ion-col>
    </ion-row>
</details>
<ion-button type="button" (click)="saveDetails()">Save</ion-button>

这是product.page.ts

...
categoryId: any;
productId: any;
minPrice: any;
maxPrice: any;
forSale: any;

saveDetails() {

    let data = {categoryId: this.categoryId, productId: this.productId, minPrice: this.minPrice, maxPrice: this.maxPrice, forSale: this.forSale};

    this.http.post('http://example.com/api/saveDetails', JSON.stringify(data))
    .subscribe((res: any) => {console.log("success");});
}

问题是我不知道如何以以下格式发送该数据,因为 API 只接受这种格式将其保存到数据库中。

"productList": [
    {
        //First
        'categoryId': 'value',
        'productId': 'value',
        'minPrice': 'value',
        'maxPrice': 'value',
        'forSale': 'value'
    },
    {
        //Second
        'categoryId': 'value',
        'productId': 'value',
        'minPrice': 'value',
        'maxPrice': 'value',
        'forSale': 'value'
    },
    ...
],

我们将不胜感激。

【问题讨论】:

  • 你能把你生成的 JSON 贴在这里吗?
  • @bZezzz 当我点击保存按钮时,我在 chrome 控制台中收到未定义的错误。因此,不会生成 JSON。
  • console.log(JSON.stringify(data)) ?
  • 响应是 {}
  • @bZezzz 你怎么看。有什么问题?

标签: angular ionic-framework


【解决方案1】:

例如,您可以使用ReplaySubject。在product.page.ts 中执行此操作:

private readonly dataSend = new ReplaySubject();

ngOnInit(): void {
  this.dataSend.pipe(
    scan((acc, current) => {
      acc.push(current)
      return acc;
    }, []), 
    debounceTime(1),
    switchMap(data => this.http.post('http://example.com/api/saveDetails', data))
).subscribe(console.log);
}

...

saveDetails() {

    const data = {categoryId: this.categoryId, productId: this.productId, minPrice: this.minPrice, maxPrice: this.maxPrice, forSale: this.forSale};

    this.dataSend.next(data);
}

...

ngOnDestroy(): void {
  this.dataSend.complete();
}

【讨论】:

  • 如果您在发送的数据中需要productList 属性:this.http.post('http://example.com/api/saveDetails', {productList: data})
  • 嗨。它没有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多