I'm trying to combine Angular2 with jQuery + Chosen plugin. It's basically an extension of the answer to this question How to use jQuery with Angular2?.
I want to dynamically update items displayed in Chosen select box. Chosen has chosen:updated
event design for this but I need to trigger it after component's view has been updated because Chosen just looks at the <option>
elements and generates its content based on them.
You can see live "working" demo: http://plnkr.co/edit/42RaphKFHPNrNYuWlrbe?p=preview
This means that if I do:
this.items.push('Another #' + this.items.length);
jQuery(this.el.nativeElement).find('select').trigger("chosen:updated");
It won't do anything because the view hasn't been updated after calling this.items.push()
.
I tried wrapping trigger("chosen:updated")
with setTimeout
to call it asynchronously which I thought could help.
this.items.push('Another #' + this.items.length);
setTimeout(() => {
jQuery(this.el.nativeElement).find('select').trigger("chosen:updated");
});
It seems like it does help but I'm wondering if this is the correct approach or just coincidence that this works?
I thought there could be some event on components called after their view has been updated but it seem like none of them do what I need.
Edit: The same problem is very well described in another question: Triggering Angular2 change detection manually.
Updated working demo: http://plnkr.co/edit/w7hHmUaD9Eqq3ffQueXp?p=preview