3

I am new to Angular. I saw similar questions here but still cant get the point. In my service i have:

banner(): Observable<String> {
    return this.http.get<String>(`${SERVER_API_URL}/api/banner`);
}

In component:

ngOnInit() {
    this.principal.identity().then(account => {
        this.account = account;
    });

    this.registerAuthenticationSuccess();
    this.craCoreService.banner().subscribe(value => this.banner = value);
}

And html:

<div class="alert alert-warning" *ngSwitchCase="false">
   {{banner}}
</div>

My service method returns simple JSON object with single String property called "banner". But in UI i always get [Object object]. I do not understand how to get value from it. Appreciate any help, thank you.

1
  • You might have to do like {{ banner.banner }} Commented Sep 18, 2018 at 12:17

2 Answers 2

8

If it's an object with the banner property, then you need to use that use property in your TS code

this.craCoreService.banner().subscribe(value => this.banner = value.banner);
Sign up to request clarification or add additional context in comments.

2 Comments

It works, thank you. But i have some trash in console now. Does it easy to fix, or can I ignore it? ERROR TypeError: Cannot read property 'banner' of undefined
this.craCoreService.banner().subscribe(value => { if (value) this.banner = value.banner; });
3

use json pipe to show the object in the template.

<div class="alert alert-warning" *ngSwitchCase="false">  {{banner | json}}</div>

Comments

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.