當我呼叫以下函式之一時,我需要更改 pageNumber 和 titulo 的引數值......但是當我呼叫這些函式時,這些引數保持不變,例如
http://miurl.com/jsonapi/views/busqueda_avanzada/informes?page=${this.pageNumber}&views-filter[titulo]=${this.titulo}; //pageNumber 回傳 0,這是初始值,但是當我呼叫 getPage() 函式時,每次呼叫它應該回傳 1 然后回傳 2 時仍然回傳 0。這個想法是用 Angular 創建一個搜索過濾器。希望你能給我一些關于我做錯了什么的線索!謝謝
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
export interface InformesCounter {
data: any;
meta: any;
count: any;
attributes: any;
links: any;
next: any;
href: any;
titulo: any;
}
@Injectable({
providedIn: 'root'
})
export class BuscadorService {
pageNumber: any = 0;
titulo: any = '';
informesNodes =`http://miurl.com/jsonapi/views/busqueda_avanzada/informes?page=${this.pageNumber}&views-filter[titulo]=${this.titulo}`;
constructor(private http: HttpClient) { }
getNodes(): Observable<InformesCounter> {
this.pageNumber = 1;
return this.http.get<InformesCounter>(`${this.informesNodes}`);
}
getFiltered(titulo: any): Observable<InformesCounter> {
this.titulo = titulo;
this.pageNumber = 1;
return this.http.get<InformesCounter>(`${this.informesNodes}`);
}
getPage(): Observable<InformesCounter> {
this.pageNumber = this.pageNumber 1;
return this.http.get<InformesCounter>(`${this.informesNodes}`);
}
ungetPage(): Observable<InformesCounter> {
this.pageNumber = this.pageNumber - 1;
return this.http.get<InformesCounter>(`${this.informesNodes}`);
}
}
uj5u.com熱心網友回復:
informesNodes當創建類實體時,您的模板字串只會被評估一次。它不會自動更新新資訊。
一個簡單的解決方案是用getter替換它,其余代碼可以保持原樣:
get informesNodes() {
return `http://miurl.com/jsonapi/views/busqueda_avanzada/informes?page=${this.pageNumber}&views-filter[titulo]=${this.titulo}`;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481414.html
標籤:javascript 有角度的 打字稿
下一篇:如何在html中提供邊距頂部空間
