0

I'm facing a problem while I want to display some values which I got from a API call in a dropdown in the fronted.

I call my endpoint like this:

component.ts

ngOnInit() {
  const values = this.backendService.getAllValues().subscribe()
}

The call works, I get the following output:

0: {name: "Data Visualisation",…}
 name: "Data Visualisation"
1: {name: "Watchning TV",…}
 name: "Watchning TV"

Now, I d'like to display those values in my dropdowon:

component.html

<scs-dropdown>
   <scs-dropdown-item *ngFor="let value of values" [value]="value.name">
   </scs-dropdown-item>
</scs-dropdown>

But nothing is displayed in the dropdown, what do I wrong?

1
  • can you create a stackblitz for the same Commented May 20, 2020 at 15:19

3 Answers 3

2

That's not how you should set the values attribute. It should be like this:

values = [];

ngOnInit() {
    this.backendService.getAllValues().subscribe(response => {
        this.values = [];
        for (const item in response) {
            this.values.push(item);
        }
    });
}

Look that values is a class attribute, not a variable defined in ngOnInit

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

5 Comments

Thanks for your answer. Unfortunately it doesn't work. I got this error message: Type 'Object' is not assignable to type 'never[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?ts(2322)
This is because your response is not an array. You should convert to an array before assign to this.values
where should I define the item?
With this solution I got the error: Argument of type 'string' is not assignable to parameter of type 'never'
Can you tell me what shows when you add console.log(response) at the beginning?
0

The Values you get from the API Must be mapped into an Array using the ' .map => ' operator then once mapped you can loop through the values on the interface

1 Comment

I think that your answer is wrong. The return type of the api call is an array.. This return type is an Subscription: this.backendService.getAllValues().subscribe()
0

Please try this and show us the console output:

values = [];

ngOnInit() {
  this.backendService.getAllValues().subscribe( arr => {
      console.info(JSON.stringify(arr, null, 4));
      values = arr
  });
}


<scs-dropdown>
   <scs-dropdown-item *ngFor="let value of values" [value]="value.name">
      {{value.label}} :-)
   </scs-dropdown-item>
</scs-dropdown>

See: https://angular.io/guide/observables-in-angular

Don't forget to unsubscribe.

1 Comment

Thanks for your answer. Unfortunately it doesn't work. I got this error message: Type 'Object' is not assignable to type 'never[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?ts(2322)

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.