I am trying to call jQuery function (i.e. initialize bootstrap selectpicker) at page load in ng2 app:
jQuery(this.elementRef.nativeElement).find("select").selectpicker({
iconBase: 'fa',
tickIcon: 'fa-check',
title: '...'
});
(assume that jQuery and elementRef are defined and it works well in other cases)
I try to call this function in ngOnInit, ngAfterContentInit, ngAfterViewInit but with no luck.
I suspect it calls before element is rendered or so.
Here is my template of select which I try to convert into bootstrap selectpicker:
<select ngControl="hashtags" [(ngModel)]="hashtags" multiple>
<option *ngFor="#hashtag of card.hashtags" [value]="hashtag.id" (click)="selectHashtag(hashtag.id)">{{hashtag.name}}</option>
</select>
Any ideas how to call it right?
UPD: seems like the issue comes from a fact that a form (and my select element) is rendered inside ngIf:
<div *ngIf="card">
...
</div>
so, at the moment of rendering page and calling ngOnInit() form is not rendered yet. I will try to work it around by applying different approach, but how would you call function AFTER http request and form render are complete?
jQuery(this.elementRef.nativeElement).find("select")return anything?jQuery []however, if I call$("select")after page load it returnsjQuery [<select class="ng-untouched ng-pristine ng-invalid">]jQuery [<select class="ng-untouched ng-pristine ng-invalid">]if I bindconsole.log(jQuery(this.elementRef.nativeElement).find("select"));to a button and call it later. Is it what you asked for?