Angular 2 fire event after ngFor

﹥>﹥吖頭↗ 提交于 2019-12-23 10:34:10

问题


I'm trying to use a jQuery plugin which replaces the default scrollbar inside some dynamic elements in Angular 2.

These elements are created using an ngFor loop, that is I cannot attach the jQuery events to the elements these are created.

The application, at some point, mutates an Observable object which iscycled inside the ngFor in order to render the view.

Now, I would like to know when Angular finishes to draw my elements so that I can run the jQuery plugin.

  • I don't want to include any javascript in the HTML template.
  • I don't want to use the ngAfterViewInit, because this hooks is fired too many times
  • I don't want to implement a setTimeout-based solution (I think it's not reliable)

I found a solution by using the following custom Pipe: at the end of the ngFor cycle in the template, I write:

{{i | FireStoreEventPipe:'EVENT_TO_BE_FIRED'}}

Where FireStoreEventPipe is something like:

transform(obj: any, args:any[]) {
    var event = args[0];
    //Fire event
    return null;
}

But this solution does not satisfy me.

Any suggestion for a better way?

Thanks


回答1:


I had a similar problem. I am using angular2 rc 1.

I solved it by creating a new component(a directive didnt work for me).

import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
   selector: 'islast',
   template: '<span></span>'
})
export class LastDirective {
   @Input() isLast: boolean;
   @Output() onLastDone: EventEmitter<boolean> = new EventEmitter<boolean>();
   ngOnInit() {
      if (this.isLast)
        this.onLastDone.emit(true);
   }
}

Then i imported in my wanted component as child component in the directives:

directives: [LastDirective],

In html:

<tr *ngFor="let sm of filteredMembers; let last = last; let i=index;" data-id="{{sm.Id}}">
     <td>
        <islast [isLast]="last" (onLastDone)="modalMembersDone()"></islast>
     </td>
 </tr>

And of course implement modalMembersDone()



来源:https://stackoverflow.com/questions/36482343/angular-2-fire-event-after-ngfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!