3

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());
}
  • 2
    i think it should be ```return this.http.post(this.commonUrlObj.commonUrl +'/saveNewCategory' + formData).map((res: any) => res.json());``` – Fateme Fazli Sep 25 '18 at 08:52
  • 1
    Possible duplicate of [JS strings "+" vs concat method](https://stackoverflow.com/questions/16124032/js-strings-vs-concat-method) – Jota.Toledo Sep 25 '18 at 09:00

3 Answers3

7

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

Arne
  • 832
  • 1
  • 5
  • 13
  • 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. – Hamzeen Hameem Feb 12 '19 at 09:59
1

remove single quotes from 'this.commonUrlObj.commonUrl'

saveNewCategory(formData){
  return this.http.post(this.commonUrlObj.commonUrl+'/saveNewCategory',formData).map((res: any) => res.json());
}
Krishna Rathore
  • 9,389
  • 5
  • 24
  • 48
0

Please remove the single quotes and it should work.

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