Вы можете сделать это следующим образом:
<h2>Mes documents</h2>
<ul>
<ng-container *ngFor="let document of documents; let i = index" >
<li *loaderAndErrorHandler="document.isLoading; wait 2000 beforeDisplaying errorMessage;">
<article (click)="getDocument(document.id, i)" class="document-label">
<img src="https://image.noelshack.com/fichiers/2019/11/7/1552854323-iconfinder-note3-254240.png" height="20px" width="20px">
<label>
<strong>{{document.label}}</strong>
</label>
</article>
<article class="document-date">
<p>{{document.date}}</p>
</article>
</li>
</ng-container>
</ul>
Метод getDocument
принимает индекс цикла в качестве дополнительного параметра, чтобы получить подходящий для обновления. Для примера вам нужно выбрать другое значение, чтобы убедиться, что документ существует в ваших данных.
import { Component, Input, ViewRef } from '@angular/core';
import { StoreService } from './services/store/store.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
documents = [
{
id: '123',
label: 'Certificat of something great',
date: '15 jun.',
isLoading: false
},
{
id: '123',
label: 'Certificat of something not so great',
date: '15 jun.',
isLoading: false
},
{
id: '123',
label: 'Certificat of something ok',
date: '15 jun.',
isLoading: false
},
];
public errorMessage = 'Erreur: Une erreur s\'est produite durant la récupération de votre document.\nVeuillez renouveler votre requête ultérieurement.'
constructor(private store: StoreService) {
this.store.notificationPublisher.subscribe(notification => {
console.log('Notification:', notification)
})
}
public getDocument = (documentId: string, documentIndex: number): void => {
this.documents[documentIndex].isLoading = true;
setTimeout(() => {
this.documents[documentIndex].isLoading = false;
}, 1000)
}
}
Я добавил только состояние для каждого документа и элемента, для которого вы выполняете цикл с *ngFor
РЕДАКТИРОВАТЬ : Вы можете проверить это здесь => https://stackblitz.com/edit/angular-hfnhg7