2

I have a small component where I have only one input

Ts component Code

 @Input() variables;

      ngOnInit() {
        console.log(this.variables);
    }
   

Where I use my component TS

envVars$: Observable<{
            names: String[],
            values: {}
          }>;

Where I use my component HTML

 <app-code  [variables]="(envVars$ | async)?.values | json"></app-code>
 

In the console log I HAVE NULL though my object is full

8
  • can you please post the whole ts code of the component? Commented Jan 19, 2018 at 15:33
  • I don't know why the async pipe returns NULL Commented Jan 19, 2018 at 15:43
  • It's working when I switch to <app-code>{{ (envVars$ | async)?.values | json }}</app-code> This is really weird!!!!!!!!!!!!! Commented Jan 19, 2018 at 15:45
  • did you try to remove the json pipe? Commented Jan 19, 2018 at 15:53
  • 1
    the log says null because you're logging ON INIT before the object has been asynchronously defined. The async pipe is working exactly as intended. if you need the object to be defined on init then you need to change your structure a little. Commented Jan 19, 2018 at 16:23

1 Answer 1

1

the log says null because you're logging ON INIT before the object has been asynchronously defined. The async pipe is working exactly as intended. if you need the object to be defined on init then you need to change your structure a little.

do this instead to ensure the observable is populated prior to component instantiation:

<app-code *ngIf="envVars$ | async as enVars" [variables]="envVars.values | json"></app-code>

the ngIf here tells angular to not instantiate the component until envVars$ emits a value.

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

2 Comments

I'll try your solution as soon as possible.
you've got something messed up somewhere along the line, check your code

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.