Angular 7多个查询字符串,如何

问题描述 投票:0回答:1

我有一个asp.net方法接受项目列表作为参数。该功能有效并已经过测试。现在Angular正在使用这种方法。我的问题是,如何从Angular中成功传递多个参数?

从本质上讲,我正在努力实现这一目标:

localhost:4200/download?item=fname&item=lname&item=cc

值从Angular材料设计复选框传递。

Asp.Net核心

[HttpGet("downloadbooks")]
public IActionResult Download(IList<string> item)
{
  //omitted for simplicity
}

角度服务

downloadfunction(item: any = []){
  return this.http.get(url + `books/downloadbooks?item=${item}`,  { headers: new HttpHeaders({'Content-Type': 'application/json',
      }), responseType: 'blob'}
    );
}

角度TS

fname;
fnameChecked = false;

lname;
lnameChecked = false;

cc;
ccChecked = false;


  onfnameChecked() {
    this.fname= ""
    if (this.fnameChecked) {
      this.fname = "fName"
    }
  }

  onlnameChecked() {
    this.lname= ""
    if (this.lnameChecked) {
      this.lname = "lName"
    }
  }

 onccChecked() {
    this.cc= ""
    if (this.ccChecked) {
      this.cc= "cc"
    }
  }

downloadBooks(){

//I want to pass multiple parameters within the dowloadfunction()
//only one parameter is passed at the moment.

 this.downloadService.downloadfunction(this.fname).subscribe(blob => {
 link.href= window.URL.createObjectURL(blob);
})
}

Angular html


    <mat-checkbox class="example-margin" name="fnameChecked" [(ngModel)]="fnameChecked" [checked]="fnameChecked" (change)="onfnameChecked()">First name</mat-checkbox>

    <br />
    <mat-checkbox class="example-margin" name="lnameChecked" [(ngModel)]=" lnameChecked " [checked]=" lnameChecked " (change)="onlnameChecked()">Last Name </mat-checkbox>
    <br />
<mat-checkbox class="example-margin" name="ccChecked" [(ngModel)]="ccChecked " [checked]=" ccChecked " (change)="onccChecked()">Credit card</mat-checkbox>

  <button mat-raised-button [mat-dialog-close]="" color="primary" (click)="downloadBooks()"><a [target]="'_blank'">Submit</a></button>

c# typescript angular-material url-routing angular7
1个回答
1
投票

您可以使用HttpParams来实现这一目标。

const item = [fname, lname, cc];
const options = {
  headers: new HttpHeaders({'Content-Type': 'application/json'}),
  params: new HttpParams().set('item', JSON.stringify(item))
};

this.http.get(url, options)
© www.soinside.com 2019 - 2024. All rights reserved.