简体   繁体   English

如何在 ngIf 语句中创建嵌入式 ngFor?

[英]How can I create an embedded ngFor inside of an ngIf statement?

What I'm Trying To Do我想做的事

I'm trying to create a page where there is a bunch of mat-expansion-panels that each have their own content.我正在尝试创建一个页面,其中有一堆垫子扩展面板,每个面板都有自己的内容。 The content is hardcoded, but rather than having each page on it's own component, I wanted to try and make one component that gets reused and interacts with a static service in order to be scalable as more content needs to be added, or if I were to connect it to a database in the future.内容是硬编码的,但是我不想让每个页面都在它自己的组件上,我想尝试制作一个可以重用并与 static 服务交互的组件,以便在需要添加更多内容时可扩展,或者如果我是将来将其连接到数据库。

What I've Tried我试过的

What I've Tried (Variation 1)我试过的(变奏1)

<article *ngIf="code$ | async as title">
    <h1>{{title.title}}</h1>
    <div *ngFor="section of title.sections | async">
        <mat-expansion-panel>
            <mat-expansion-panel-header>
                <mat-panel-title>
                    {{sTitle}}
                </mat-panel-title>
                <mat-panel-description>
                    {{sDesc}}
                </mat-panel-description>
            </mat-expansion-panel-header>
            <div>
                {{sContent}}
            </div>
        </mat-expansion-panel>
    </div>
</article>

What I've Tried (Variation 2)我试过的(变奏2)

<article *ngIf="code$ | async as title">
    <h1>{{title.title}}</h1>
    <div *ngFor="section of title.sections | async as section">
        <mat-expansion-panel>
            <mat-expansion-panel-header>
                <mat-panel-title>
                    {{sTitle}}
                </mat-panel-title>
                <mat-panel-description>
                    {{sDesc}}
                </mat-panel-description>
            </mat-expansion-panel-header>
            <div>
                {{sContent}}
            </div>
        </mat-expansion-panel>
    </div>
</article>

What I've Tried (Variation 3)我试过的(变奏3)

<article *ngIf="code$ | async as title">
    <h1>{{title.title}}</h1>
    <div *ngFor="section of title.sections | async">
        <mat-expansion-panel>
            <mat-expansion-panel-header>
                <mat-panel-title>
                    {{this.sTitle}}
                </mat-panel-title>
                <mat-panel-description>
                    {{this.sDesc}}
                </mat-panel-description>
            </mat-expansion-panel-header>
            <div>
                {{this.sContent}}
            </div>
        </mat-expansion-panel>
    </div>
</article>

What I've Tried (Variation 4)我试过的(变奏4)

<article *ngIf="code$ | async as title">
    <h1>{{title.title}}</h1>
    <div *ngFor="section of title.sections | async">
        <mat-expansion-panel>
            <mat-expansion-panel-header>
                <mat-panel-title>
                    {{this.section.sTitle}}
                </mat-panel-title>
                <mat-panel-description>
                    {{this.section.sDesc}}
                </mat-panel-description>
            </mat-expansion-panel-header>
            <div>
                {{this.section.sContent}}
            </div>
        </mat-expansion-panel>
    </div>
</article>

Other Code其他代码

Snippet of Component.ts (everything is imported fine at the top) Component.ts 的片段(一切都在顶部很好地导入)

code$: any;
  constructor(
    private pcts: StaticService,
    private route: ActivatedRoute
  ){}
  ngOnInit(){
    this.code$ = this.route.paramMap.pipe(
      map(params => {
        const title = params.get('slug');
        return this.pcts.pctSearch(title);
      })
    )
  }

Service.ts服务.ts

export interface tSect {
  sTitle: string;
  sDesc: string;
  sContent: string;
}
export interface pct {
  id: string;
  title: string;
  sections: tSect[];
}
...
{
  id: "...",
  title: "...",
  sections: [
     {sTitle: 'Test', sDesc: 'Test', sContent: 'test'},
     {sTitle: 'Tes2t', sDesc: 'Test2', sContent: 'test2'},
  ],
},
...
pctSearch(slug: string) {
    var __FOUND = -1;
    for(var t = 0; t < this.pctarr.length; t++) {
      if(this.pctarr[t].id == slug) {
        __FOUND = t;
        return this.pctarr[__FOUND];
      }
    }
  }

What Happens?怎么了?

I get an error saying that the property (sContent, sTitle, or sDesc) doesn't exist on the component.我收到一条错误消息,指出组件上不存在该属性(sContent、sTitle 或 sDesc)。

Expected Result预期结果

The section should show a list of mat-expansion-panel(s) with the sections from the array in the service.该部分应显示一个 mat-expansion-panel(s) 列表,其中包含服务中阵列中的部分。

Put the *ngFor on the mat-expansion-panel and take out the | async将 *ngFor 放在 mat-expansion-panel 上并取出| async | async in the *ngFor, since you have already applied the pipe to the title in the *ngIf. *ngFor 中的| async ,因为您已经将 pipe 应用于 *ngIf 中的title

Edit:编辑:

Your pctSearch function should be:您的 pctSearch function 应该是:

pctSearch(slug: string) {
    return this.pctarr.filter(pct => pct.title === slug);
}

Keep in mind this will return you an array of pct's that match the slug.请记住,这将返回一个与 slug 匹配的 pct 数组。 So if you just want the first element than just grab that first index.因此,如果您只想要第一个元素,而不仅仅是获取第一个索引。

Edit 2:编辑2:

It was just a small error, you were missing "let" in your *ngFor.这只是一个小错误,您的 *ngFor 中缺少“let”。 You also need to use the actual element in the loop when asking for the properties like sTitle, etc. Also in your component where you declare code$ you should also declare it's type instead of any to Observable (change any to whatever your type is).在请求诸如 sTitle 等属性时,您还需要在循环中使用实际元素。此外,在您声明 code$ 的组件中,您还应该将其类型而不是 any 声明为 Observable(将 any 更改为您的任何类型) .

So you're template would look like this:所以你的模板看起来像这样:

<mat-expansion-panel *ngFor="let section of title.sections">
    <mat-expansion-panel-header>
        <mat-panel-title>
            {{section.sTitle}}
        </mat-panel-title>
        <mat-panel-description>
            {{section.sDesc}}
        </mat-panel-description>
    </mat-expansion-panel-header>
    <div>
        {{section.sContent}}
    </div>
</mat-expansion-panel>

Also in your component you should declare code$ like this:同样在您的组件中,您应该像这样声明 code$ :

code$: Observable<any>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM