How to concat url in Angular 6?

10,375

Solution 1

I'd advice you to use a string literal. Using `, resulting in `${this.commonUrlObj.commonUrl}/saveNewCategory`

Solution 2

remove single quotes from 'this.commonUrlObj.commonUrl'

saveNewCategory(formData){
  return this.http.post(this.commonUrlObj.commonUrl+'/saveNewCategory',formData).map((res: any) => res.json());
}
Share:
10,375

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have common-class has commonUrl, this commonUrl i used in category.service.ts but it not concat in service.ts how to concat this commonUrl in angular 6?

    common-class.ts

    export class CommonClass {
      constructor(public commonUrl : string = 'http://localhost:3000'){};
    }
    

    category.service.ts

    import { CommonClass } from '../classes/common-class';
    commonUrlObj : CommonClass = new CommonClass();
    
    saveNewCategory(formData){
      return this.http.post('this.commonUrlObj.commonUrl'+''+'/saveNewCategory',formData).map((res: any) => res.json());
    }
    
    getCategoryDetails(param){
      return this.http.post('this.commonUrlObj.commonUrl'+''+'getCategoryDetails',param).map((res: any) => res.json());
    }
    
    • Fateme Fazli
      Fateme Fazli over 5 years
      i think it should be return this.http.post(this.commonUrlObj.commonUrl +'/saveNewCategory' + formData).map((res: any) => res.json());
    • Jota.Toledo
      Jota.Toledo over 5 years
      Possible duplicate of JS strings "+" vs concat method
  • Hamzeen Hameem
    Hamzeen Hameem about 5 years
    using the above template literal is the recommended way. If you use +, it will result in a code smell defect if you are running a tool like Sonar.