0

I have used angualr 7 and i m passing the value from the service class of angular as:

executeHelloWorldBeanServiceWithPathVariable(name){
    console.log("name coming from here"+name);
    return this.httpClient.get<HelloWorldBean>('http://localhost:8080/hello-world/path-variable/${name}');
     console.log("hello world bean service executed");
   }

The name is being printed in console as I tested:

console.log("name coming from here"+name);

Its being printed in console as here is no problem.

enter image description here

In my spring boot i declared as:

@GetMapping(path="/hello-world/path-variable/{name}")  
    public HelloWorldBean helloWorldBeanPathVariable(@PathVariable("name") String name) {
        System.out.print("name is"+name);
        return new HelloWorldBean(String.format("Hello world %s", name));
    }

The name parameter which I pass from angular is not being printed as I tried to debug using:

System.out.print("name is"+name);

But it is showing in el expression

enter image description here

So in my UI I am getting:

enter image description here

2 Answers 2

2

It seems like you aren't using the actual template literal syntax on your executeHelloWorldBeanServiceWithPathVariable method. I am assuming you are trying to use that because of the ${} expression.

This might be the reason why the request didn't get parsed correctly. You should be using back ticks(`) instead of single or double quotes.

executeHelloWorldBeanServiceWithPathVariable(name){
  return this.httpClient.get<HelloWorldBean>(`http://localhost:8080/hello-world/path-variable/${name}`);
}

In addition, to actually return the observables from the API request, you will have to subscribe() to it on the component that requires it.

For instance,

constructor(
  private yourService: YourService,
) { }

ngOnInit() {
  this.yourService.executeHelloWorldBeanServiceWithPathVariable('someName').subscribe(res => {
    console.log(res);
    // do the rest here
  })
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ashwinkarki glad to help!
-1

use backticks ` instead using ' or " for concatanation whenever work on typescript. specifically where you evaluate some value like "my string"+${myname}

1 Comment

this is not an answer to the question - more like a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.