我是Ionic,Angular和Typescript的新手,是否有可能将输入的数字作为键值之一添加到API URL中?
例如,这是我的输入html
<ion-input type="text" placeholder="Enter Number"></ion-input>
例如,这是我的API URL。
https://myapi.com/FoodDeliveryTracking?MyNumber = [输入]&userName = myuser&password = mypass
这是我的data.services.ts,用于从我的API网址中获取数据。
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
apiUrl = 'http://127.0.0.1:8080/https://myapi.com/FoodDeliveryTracking?MyNumber=[value from input]&userName=myuser&password=mypass';
apiKey = ''; // <-- Enter your own key here!
constructor(private http: HttpClient){}
getRemoteData(){
let headers = new HttpHeaders({'apiKey': this.apiKey});
headers.append('Accept','application/json');
headers.append('X-Requested-With','XMLHttpRequest');
headers.append('content-type','application/json');
return this.http.get(this.apiUrl, {headers: headers});
}
}
let myParams = {'foo': 'hi there', 'bar': '???'};
let u = new URLSearchParams(myParams).toString();
console.log(u);