Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

*ngFor directive isn't working with mat-expansion panel. Need solution

when I am adding *ngFor with mat-expansion panel in my html file, it is not letting it work. The moment I remove it, it works fine. Here are some screenshots of the files.

I tried with and without *ngfor and without it, that works but with it, it doesnt.

html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<mat-accordion>
    <mat-expansion-panel *ngFor = "let post of posts">
        <mat-expansion-panel-header>
        {{ post.title }}
        </mat-expansion-panel-header>
       {{ post.content }}
    </mat-expansion-panel>
</mat-accordion> 

ts file

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-post-list',
  templateUrl: './post-list.component.html',
  styleUrls: ['./post-list.component.css']
})
export class PostListComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  posts : [
    { title : "this is first title" , content : "This is 1 content" },
    {title : "this is second title", content : "This is 2 content"},
    {title : "this is third title", content : "This is 3 content"}
  ];
}

when I am adding *ngFor with mat-expansion panel in my html file, it is not letting it work. The moment I remove it, it works fine. Here are some screenshots of the files.

I tried with and without *ngfor and without it, that works but with it, it doesnt.

>Solution :

You defined your post array in wrong way. : is used to declare the type of property so use = instead

posts = [ 
    { title : "this is first title" , content : "This is 1 content" },
    {title : "this is second title", content : "This is 2 content"},
    {title : "this is third title", content : "This is 3 content"}
  ];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading