2017-07-10 16 views
0

私のコードは、リクエストURLとURLのparamsを結合することができません。今ここに何が起こるかhttpconfigは、以下に示すように

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()の場合、最後のURLは http://myAPi.com/items/categories?_id=this.restaurent._idです。しかし、私はhttp://myAPi.com/items/categories/this.restaurent._idのようなURLを望んでいます。NetworkService.tsファイルに何かがありますか?

答えて

1

dataをクエリ文字列として渡しています。クエリ文字列として渡す代わりに、template literalsを使用してそれを形成できます。 dataはパラメータとして渡されないことに注意してください。

this.appService.networkService.get<any>(`${this.appService.appConstant.appUrls.getFoodCategories}/${this.restaurent._id}`) 
    .then((response) => {   
    }, 
    (error) => { }); 
} 
関連する問題