如何在Angular中将参数传递给服务类的查询方法?

时间:2019-01-15 07:51:32

标签: angular

这部分代码由jHipster生成。

invoice.component.ts

@Component({
    selector: 'jhi-invoice',
    templateUrl: './invoice.component.html'
})

export class InvoiceComponent implements OnInit, OnDestroy {


  loadAll() {
        this.invoiceService
            .query({
            page: this.page - 1,
            size: this.itemsPerPage,
            sort: this.sort()
        })
        .subscribe(
            (res: HttpResponse<IInvoice[]>) =>  this.paginateInvoices(res.body, res.headers),
            (res: HttpErrorResponse) => this.onError(res.message)
        );
}

 ngOnInit() {
       this.loadAll();
       this.accountService.identity().then(account => {
       this.currentAccount = account;
       });
       this.registerChangeInInvoices();
   }

}

invoice.service.ts

@Injectable({ providedIn: 'root' })
    export class InvoiceService {
    public resourceUrl = SERVER_API_URL + 'api/invoices';

    constructor(protected http: HttpClient) {}    

    find(id: number): Observable<EntityResponseType> {
        return this.http
           .get<IInvoice>(`${this.resourceUrl}/${id}`, { observe: 'response' })
           .pipe(map((res: EntityResponseType) =>   this.convertDateFromServer(res)));
}

    query(req?: any): Observable<EntityArrayResponseType> {
        const options = createRequestOption(req);
        return this.http
            .get<IInvoice[]>(this.resourceUrl, { params: options, observe: 'response' })
            .pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
    }
}

该代码允许我获取所有发票,但是我想传递client_id来仅选择该客户的发票。请帮助实现此功能。

1 个答案:

答案 0 :(得分:0)

您可以在查询中使用新参数。

load(number: client_id) {
        this.invoiceService
            .query({
            page: this.page - 1,
            size: this.itemsPerPage,
            sort: this.sort(),
            client_id: client_id
        })
        .subscribe(...);
}