0

I have a function in my component as

  missionSelected(event){
    this.category = event.option.value.children;
    console.log(this.category);
  }

This console log i am getting as:

    (3) [{…}, {…}, {…}]

But as i am using this variable in my template as:

    <mat-list-option *ngFor="let cat of category" [value]="cat">
        <div>{{cat.name}}</div>
    </mat-list-option>

This one giving me error as:

    find a differ supporting object '[object Object]' of type 'object'.
NgFor only supports binding to Iterables such as Arrays.

I tried to reinitialize the same object in array variable but i am getting same error. Many ways i tried but not getting proper solution.

adding console screenshot

6
  • Please provide your html code and ts, as the code you have provided, seems to be fine. Commented Jul 16, 2020 at 11:52
  • what is there inside (3) [{…}, {…}, {…}] Commented Jul 16, 2020 at 12:07
  • can you elaborate what you got in the console, (3) [{…}, {…}, {…}] may be a mock data would help us to figure out? Commented Jul 16, 2020 at 12:09
  • can you please console.log(this.category[0]); Commented Jul 16, 2020 at 12:16
  • yes {path: "/home/rajesh/geospace/GIS", name: "GIS", children: Array(4), size: 18502537, type: "directory"} children: (4) [{…}, {…}, {…}, {…}] name: "GIS" path: "/home/rajesh/geospace/GIS" size: 18502537 type: "directory" __proto__: Object Commented Jul 16, 2020 at 12:19

3 Answers 3

2

You either need to check in the template if the array is defined

<ng-container *ngIf="category">
  <mat-list-option *ngFor="let cat of category" [value]="cat">
    <div>{{cat.name}}</div>
  </mat-list-option>
</ng-container>

or initialize the array in the definition.

category: Array<any> = [];

Update: keyvalue pipe

I am not sure if you're actually looping through an array. If in any case it's an object, you could use keyvalue pipe with the *ngFor directive.

<mat-list-option *ngFor="let cat of category | keyvalue" [value]="cat.value">
  <div>{{cat.key}}</div>
  <div>{{cat.value}}</div>
</mat-list-option>
Sign up to request clarification or add additional context in comments.

4 Comments

From one of your other comments, you say the definition is category: [];. That wrongfully defines the variable to be of type [], not the JS Array. It needs to be category: Array<any>.
As per your recommendation i changed the definition from category: []; to category: Array<any> = []; but getting same error
Please provide a screenshot of your console log. Also please check if it works with the keyvalue pipe as mentioned in the update. If so, they the response is actually an object.
added screenshots of my console in question section . if i am using keyvalue pipe i am getting another error as ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngForOf: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]'. Current value: 'ngForOf: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]'.
0

Maybe the problem is the initial value of category. Have you initialized it to [] for example? If the initial value is null or other object *ngFor can't work with it as the message says.

1 Comment

yes i have already declare that in my component as category: [];
0

Thank you so much for all answer but i only was doing the mistake here: in my template i am using reference #category which is same as my category variable as shown below:

<mat-selection-list #category [multiple]="false">           
<mat-list-option *ngFor="let cat of category" [value]="cat.value">
<mat-icon mat-list-icon>folder</mat-icon>             
<div mat-line>{{cat.name}}</div>          
</mat-list-option>
</mat-selection-list> 

I just changed that reference name from #category to #reffer and it worked.....

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.