我的代码如下所示:
RestaurentInfoController.ts
module App.Controller {
import Services = Core.Services;
import Shared = Core.Shared;
export class RestaurentInfoController extends BaseController {
public userName: any;
public password: any;
public validUserName: boolean = false;
public validPassword: boolean = false;
public restaurentName: any = [];
public checkBox: any;
public restaurent: any;
public foodTruckList: any = [];
public foodCategories: any = [];
public drinkCategories: any = [];
static $inject: Array<string> = ['baseAppService', 'userAuthorizationService', 'storageService', 'eventService',];
constructor(
appService: Services.BaseAppService
, public userAuthorizationService: Services.UserAuthorizationService,
public storageService: Services.StorageService,
public eventService: Services.AppEventBusService) {
super(appService);
this.getRestaurentList();
}
routeTo(view) {
this.appService.routerService.routeToPage(view);
}
getRestaurentList = (): void => {
this.appService.networkService.get<any>(this.appService.appConstant.appUrls.getFoodTruckName).then((response) => {
this.foodTruckList = response.data;
},
(error) => { });
}
changeStatus = (): void => {
if (this.checkBox === '1') {
this.getFoodCategories();
}
else if (this.checkBox === '2') {
this.getDrinkCategories();
}
}
getFoodCategories = (): void => {
console.log("rest " + this.restaurent);
var data = {
_id: 'this.restaurent._id'
}
this.appService.networkService.get<any>(this.appService.appConstant.appUrls.getFoodCategories,data).then((response) => {
this.foodCategories = response.data;
console.log('popuar Items Loaded', this.foodCategories);
},
(error) => { });
}
getDrinkCategories = (): void => {
var data = {
_id: this.restaurent._id
}
this.appService.networkService.get<any>(this.appService.appConstant.appUrls.getFoodTruckName, data).then((response) => {
this.foodTruckList = response.data;
console.log('popuar Items Loaded', this.foodTruckList);
},
(error) => { });
}
}
}
NetworkService.ts
/// <reference path="../../../typings/app.d.ts" />
/// <reference path="../../../typings/tsd.d.ts" />
module Core.Services {
export class NetworkService {
static $inject: Array<string> = ['$http', '$log', '$q', 'appConstant', 'storageService'];
constructor(public $http: ng.IHttpService, public $log: ng.ILogService, public $q: ng.IQService,
public appConstant: Shared.AppConstants, public storageService: Services.StorageService) { }
private onError(error: any): void {
// generic handling for all error, including authorization realted stuff
this.$log.error(error);
}
private getConfig(url: string, config?: ng.IRequestShortcutConfig): ng.IRequestConfig {
var httpConfig = <ng.IRequestConfig>{};
if (config != null) {
angular.extend(httpConfig, config);
}
var token = this.storageService.getItem(this.appConstant.keys.token, false);
if (token != null) {
var tokenHeader = {
'Authorization': "Bearer " + token
};
var currentHeaders = httpConfig.headers;
if (currentHeaders) {
httpConfig.headers = angular.extend(currentHeaders, tokenHeader);
} else {
httpConfig.headers = tokenHeader;
}
}
httpConfig.url = url;
return httpConfig;
}
private getOrDelete<T>(url: string, methodType: string, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
var httpConfig = this.getConfig(url, config);
httpConfig.method = methodType;
return this.getResponse<T>(httpConfig);
}
private getResponse<T>(config: ng.IRequestConfig): ng.IPromise<T> {
var deferred = this.$q.defer();
config.headers
this.$http(config).success(
(result: any) => {
deferred.resolve(result);
}).error((error, errorCode) => {
this.onError(error);
deferred.reject(new Core.Models.HttpError(error, errorCode));
});
return deferred.promise;
}
get<T>(url: string, data?: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
var httpConfig = this.getConfig(url, config);
httpConfig.method = "GET";
if (data) {
httpConfig.params = data;
}
return this.getResponse(httpConfig);
}
delete<T>(url: string, data?: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
var httpConfig = this.getConfig(url, config);
httpConfig.method = "DELETE";
if (data) {
httpConfig.params = data;
}
return this.getResponse(httpConfig);
}
post<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
var httpConfig = this.getConfig(url, config);
httpConfig.method = "POST";
httpConfig.data = jQuery.param(data);
httpConfig.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
return this.getResponse<T>(httpConfig);
}
put<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
var httpConfig = this.getConfig(url, config);
httpConfig.method = "PUT";
httpConfig.data = data;
return this.getResponse<T>(httpConfig);
}
patch<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
var httpConfig = this.getConfig(url, config);
httpConfig.method = "PATCH";
httpConfig.data = data;
return this.getResponse<T>(httpConfig);
}
}
}
现在这里发生的是getFoodCategories()
,我得到了最终网址
http://myAPi.com/items/categories?_id=this.restaurent._id
。但是我想要这样的网址http://myAPi.com/items/categories/this.restaurent._id
。我的NetworkService.ts
文件中是否缺少任何内容?
答案 0 :(得分:1)
您正在传递data
作为查询字符串。您可以使用template literals形成查询字符串,而不是将其作为查询字符串传递。请注意,data
不作为参数传递。
this.appService.networkService.get<any>(`${this.appService.appConstant.appUrls.getFoodCategories}/${this.restaurent._id}`)
.then((response) => {
},
(error) => { });
}