0

I am trying to pass a variable in my get request in Angular 7. how do you correctly set a variable in the middle of the url;

  export class dataService {

  constructor(private http: HttpClient) { }
  getUsers(userName){

    return this.http.get('/***/****/***/${userName}/keychain****);
  }
}
Flash
  • 924
  • 3
  • 22
  • 44

3 Answers3

4

Use Template Strings. You can attach your variables using the dollar syntax ${your_variable}.

this.http.get(`/services/cxadmin/${userName}/japplegate/keychain');
kklimczak
  • 623
  • 4
  • 10
1

You can do it this way

export class dataService {

  constructor(private http: HttpClient) { }
  getUsers(userName){

    return this.http.get(`/services/admin/${userName}/japplegate/key`);
  }
}
Flash
  • 924
  • 3
  • 22
  • 44
  • You might want to explain what you did. A difference in punctuation is not necessarily obvious, and some fonts can make it worse. – Michail Feb 27 '19 at 21:03
0

Wow can't belive the issue is you have to use backticks: You need to use backticks (otherwise known as "grave accents" - which you'll find next to the 1 key) - rather than single quotes - to create a template literal.

Backticks are common in many programming languages but may be new to JavaScript developers. :ES6 / ECMA6 template literals - not working

Flash
  • 924
  • 3
  • 22
  • 44