【问题标题】:Angular 4 - Bind jQuery event (click) after *ngfor created dynamic componentsAngular 4 - *ngfor 创建动态组件后绑定 jQuery 事件(单击)
【发布时间】:2017-11-23 20:44:08
【问题描述】:

我有一种情况,我认为下面的代码

    <div class="item" *ngFor="let property of propertiesRecent; let i=index">
       <app-property [property]="property" (propertyCreated)="onPropertyCreated()"></app-property></div>

现在 app-property 看起来像这样:

 <span class="col-lg-2 socialShare">
<fa name="share-alt" size="3"></fa>
</span>

我想在上面绑定一个 jquery 事件,但是一旦更新了 propertiesRecent 的值后 DOM 更新了,我就会松开 jquery click 事件列表器..

$('.socialShareIcons').hide()

我该怎么做?如果您检查 ngFor 我尝试了自定义事件,我在 app-property 组件中都尝试了 AfterViewChecked 但没有帮助...

【问题讨论】:

  • 你为什么在这里使用 Jquery?需要了解有关此问题的更多详细信息。
  • 什么元素有socialShareIcons类?为什么不使用角度事件?

标签: angular typescript angular-components


【解决方案1】:

有一个类似的问题,这是 github 上的 link 提供的解决方案和各自的讨论

简而言之:

<div #label *ngFor="...

Component:
Class SuperComponent {
ViewChild('label')
public label: any;
...
ngAfterViewInit() {
this.handleEndOfNgfor();
this.label.changes.subscribe(()=>this.handleEndOfNgfor());
}
private handleEndOfNgfor() 
console.log('hooray!');
}
}

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多