0

Most of the examples I found online shows how to use the async pipe with either an array or an array of objects as a response from a URL API call.

The situation is that sometimes you are not gonna get an array objects but you'll get an object that contains a mix of objects and arrays inside. and you will still want to use | async inside your html. like this one

In the following Stackblitz , I added [data]="remoteData | async", and my table wont show any data. getting the following error message:

Error: InvalidPipeArgument: '[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Needless to say, if I take it off the data will show.

Obviously, I know my method works for an array as a response, in this case the response is an object. I know you are suppose to use Observables (behaviorSubject) to make this work, but having a hard time trying to get it right.

So, can I keep the async pipe? even tough the response is an object or do it need to subscribe and unsubscribe manually?

What I've tried so far: line 18 inside my service

this.listSource = apiCall;
    return this.listSource;

but I get a linting error saying:

'Observable' is missing the following properties from type 'BehaviorSubject': _value, value, getValue, next, and 9 more.

3
  • Are you sure that remoteData is Observable? Commented Feb 10, 2019 at 23:45
  • actually I'm not. but still doesnt work If I change it to any Commented Feb 10, 2019 at 23:48
  • You should read the docs on the async pipe: angular.io/guide/pipes#the-impure-asyncpipe. It is meant to be used with a Promise or Observable. Commented Feb 10, 2019 at 23:57

2 Answers 2

1

In your controller remoteData is not Observable, but actual value.

ngOnInit() {
  this.remoteData = this.remoteService.getData();
}

Now it is Observable and async pipe will work.

Sign up to request clarification or add additional context in comments.

1 Comment

nevermind, added data]="(remoteData | async)?.value" on html and that works
0

You don't need to use async pipe in your case, just checked your code, remove your async pipe and things are good. you can read more about how to use async pipe and why you need to in this tutorial https://angular.io/tutorial/toh-pt6#asyncpipe

1 Comment

so you're saying async pipe is only for arrays ?, so if you have an object then you just cant use it and you must unsubscribe manually to avoid a memory leak...care to explain ?

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.