gpt4 book ai didi

javascript - 我在 Angular HTML 模板中做错了什么?

转载 作者:行者123 更新时间:2023-12-01 00:30:41 26 4
gpt4 key购买 nike

当谈到可观察时,我对 Angular 还不太适应,所以我正在训练自己

我正在编写一个管理拳击俱乐部的小应用程序

我想显示当前所选拳击手最近参加的 3 节类(class)

所以我尝试了以下方法

 <div class="col-6">
<div *ngIf="lessons">
<div *ngIf="(lessons|async).length > 0">
<h2>les 3 derniere lessons </h2>
<hr>
<ul class="list-group" style="user-select: auto;">
<li *ngFor=" let lesson in (lessons | async) | slice:0:3; let i=index "
class="list-group-item d-flex justify-content-between align-items-center"
style="user-select: auto;">
<a [routerLink]="['/lesson', lesson.id, 'view' ]">
<span class="badge badge-primary badge-pill" style="user-select: auto;">{{i}}</span>
{{lesson.detail}}
</a>

</li>
</ul>
</div>

</div>
<div *ngIf="versements">
<div *ngIf="(versements|async).length > 0">
<h2>les 3 derniere versements </h2>
<hr>
<ul class="list-group" style="user-select: auto;">
<li *ngFor=" let versement in (versements | async ) | slice:0:3; let i=index "
class="list-group-item d-flex justify-content-between align-items-center"
style="user-select: auto;">
<a [routerLink]="['/lesson', lesson.id, 'view' ]">
<span class="badge badge-primary badge-pill" style="user-select: auto;">{{i}}</span>
{{versement.montant?}} : {{versement.date | date :'short'}}
</a>

</li>
</ul>
</div>
</div>

</div>

其中组件ts代码如下:

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

import { IBoxer } from 'app/shared/model/boxer.model';
import { JhiDataUtils, JhiAlertService } from 'ng-jhipster';
import { Observable, of } from 'rxjs';
import { MAN_PICTURE_PATH } from 'app/shared/constants/input.constants';
import { IVersement } from 'app/shared/model/versement.model';
import { VersementService } from '../versement/versement.service';
import { LessonService } from '../lesson/lesson.service';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { ILesson } from 'app/shared/model/lesson.model';

@Component({
selector: 'jhi-boxer-detail',
templateUrl: './boxer-detail.component.html'
})
export class BoxerDetailComponent implements OnInit {
boxer: IBoxer;
versements: Observable<IVersement[]>;
lessons : Observable<ILesson[]>;
manPicturePath: any;

constructor(
protected jhiAlertService: JhiAlertService,
protected activatedRoute: ActivatedRoute,
protected dataUtils: JhiDataUtils,
protected versementService: VersementService,
protected lessonService: LessonService
) {

}

ngOnInit() {
this.manPicturePath = MAN_PICTURE_PATH;

this.activatedRoute.data.subscribe(({ boxer }) => {
this.boxer = boxer;
});


this.versementService
.query({ boxerID: this.boxer.id }, this.versementService.BY_BOXER_ID)
.subscribe(
(res: HttpResponse<IVersement[]>) => this.fetchVersementsDatas(res.body),
(err: HttpErrorResponse) => this.JhiAlertError(err)
);


this.lessonService
.query( {boxerID: this.boxer.id},this.lessonService.BY_BOXER_ID)
.subscribe(
(res:HttpResponse<ILesson[]>) => this.fetchLessonsDatas(res.body),
(err:HttpErrorResponse) => this.JhiAlertError(err)
);

}

fetchLessonsDatas(lessons){
this.lessons = of(lessons);
}

fetchVersementsDatas(versements?: IVersement[]) {
this.versements = of(versements);
}

JhiAlertError(errorMessage: any) {
this.jhiAlertService.error(errorMessage, null, null);

}

byteSize(field: any) {
return this.dataUtils.byteSize(field);
}

openFile(contentType: any, field: any) {
return this.dataUtils.openFile(contentType, field);
}

previousState() {
window.history.back();
}
}

我的模板处理不当,但我不知道在哪里

这是我的控制台错误提示

core.js?f18e:6014 ERROR Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'ngForIn' since it isn't a known property of 'li'. (" > 0"> <h2>les 3 derniere lessons </h2> <hr> <ul class="list-group" style="user-select: auto;"> <li [ERROR ->]*ngFor=" let lesson in (lessons | async) | slice:0:3; let i=index " class="list-group-item d-flex jus"): ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:1790
Property binding ngForIn not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("ngth > 0"> <h2>les 3 derniere lessons </h2> <hr> <ul class="list-group" style="user-select: auto;"> [ERROR ->]<li *ngFor=" let lesson in (lessons | async) | slice:0:3; let i=index " class="list-group-item d-flex"): ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:1786
Parser Error: Unexpected end of expression: {{versement.montant?}} : {{versement.date | date :'short'}} at the end of the expression [ {{versement.montant?}} : {{versement.date | date :'short'}} ] in ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:2622 (".id, 'view' ]"> <span class="badge badge-primary badge-pill" style="user-select: auto;">{{i}}</span>[ERROR ->] {{versement.montant?}} : {{versement.date | date :'short'}} </a> </li> </ul> </div> </div> </div> </"): ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:2622
Parser Error: Conditional expression versement.montant? requires all 3 expressions at the end of the expression [ {{versement.montant?}} : {{versement.date | date :'short'}} ] in ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:2622 (".id, 'view' ]"> <span class="badge badge-primary badge-pill" style="user-select: auto;">{{i}}</span>[ERROR ->] {{versement.montant?}} : {{versement.date | date :'short'}} </a> </li> </ul> </div> </div> </div> </"): ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:2622
Can't bind to 'ngForIn' since it isn't a known property of 'li'. ("0"> <h2>les 3 derniere versements </h2> <hr> <ul class="list-group" style="user-select: auto;"> <li [ERROR ->]*ngFor=" let versement in (versements | async ) | slice:0:3; let i=index " class="list-group-item d-f"): ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:2310
Property binding ngForIn not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("h > 0"> <h2>les 3 derniere versements </h2> <hr> <ul class="list-group" style="user-select: auto;"> [ERROR ->]<li *ngFor=" let versement in (versements | async ) | slice:0:3; let i=index " class="list-group-item"): ng:///BoxingBoxerModule/BoxerDetailComponent.html@0:2306

最佳答案

要进行迭代,您需要使用以下语法*ngFor = "let value of value":

<li *ngFor="let versement of (versements | async ) | slice:0:3; let i=index">
...
</li>

As Angular docs says:

A structural directive that renders a template for each item in a collection. The directive is placed on an element, which becomes the parent of the cloned templates.

<li *ngFor="let user of userObservable | async as users; index as i; first as isFirst">
{{i}}/{{users.length}}. {{user}} <span *ngIf="isFirst">default</span>
</li>

关于javascript - 我在 Angular HTML 模板中做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58571870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com