En Angular 1, he escrito una directiva personalizada ("repetidor listo") para usar con ng-repeat para invocar un método de devolución de llamada cuando se haya completado la iteración:
if ($scope.$last === true) { $timeout(() => { $scope.$parent.$parent.$eval(someCallbackMethod); }); }Uso en marcado:
<li ng-repeat="item in vm.Items track by item.Identifier" repeater-ready="vm.CallThisWhenNgRepeatHasFinished()"> ¿Cómo puedo lograr una funcionalidad similar con ngFor en Angular 2?
Puede usar @ViewChildren para ese propósito
@Component({ selector: 'my-app', template: ` <ul *ngIf="!isHidden"> <li #allTheseThings *ngFor="let i of items; let last = last">{{i}}</li> </ul> <br> <button (click)="items.push('another')">Add Another</button> <button (click)="isHidden = !isHidden">{{isHidden ? 'Show' : 'Hide'}}</button> `, }) export class App { items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; @ViewChildren('allTheseThings') things: QueryList<any>; ngAfterViewInit() { this.things.changes.subscribe(t => { this.ngForRendred(); }) } ngForRendred() { console.log('NgFor is Rendered'); } }La respuesta original está aquí https://stackoverflow.com/a/37088348/5700401
Puedes usar algo como esto ( ngFor variables locales ):
<li *ngFor="#item in Items; #last = last" [ready]="last ? false : true">Luego puede interceptar cambios de propiedad de entrada con un setter
@Input() set ready(isReady: boolean) { if (isReady) someCallbackMethod(); }Para mí funciona en Angular2 usando Typescript.
<li *ngFor="let item in Items; let last = last"> ... <span *ngIf="last">{{ngForCallback()}}</span> </li>Entonces puedes manejar el uso de esta función.
public ngForCallback() { ... }