How to use jQuery with Angular? -


can tell me, how use jquery angular?

class mycomponent {     constructor() {         // how query dom element here?     } } 

i'm aware there workarounds manipulating class or id of dom element upfront, i'm hoping cleaner way of doing it.

using jquery angular2 breeze compared ng1. if using typescript first reference jquery typescript definition.

tsd install jquery --save or typings install dt~jquery --global --save 

typescriptdefinitions not required since use any type $ or jquery

in angular component should reference dom element template using @viewchild() after view has been initialized can use nativeelement property of object , pass jquery.

declaring $ (or jquery) jquerystatic give typed reference jquery.

import {bootstrap}    '@angular/platform-browser-dynamic'; import {component, viewchild, elementref, afterviewinit} '@angular/core'; declare var $:jquerystatic;  @component({     selector: 'ng-chosen',     template: `<select #selectelem>         <option *ngfor="#item of items" [value]="item" [selected]="item === selectedvalue">{{item}} option</option>         </select>         <h4> {{selectedvalue}}</h4>` }) export class ngchosencomponent implements afterviewinit {     @viewchild('selectelem') el:elementref;     items = ['first', 'second', 'third'];     selectedvalue = 'second';      ngafterviewinit() {         $(this.el.nativeelement)             .chosen()             .on('change', (e, args) => {                 this.selectedvalue = args.selected;             });     } }  bootstrap(ngchosencomponent); 

this example available on plunker: http://plnkr.co/edit/nq9lnk?p=preview

tslint complain chosen not being property on $, fix can add definition jquery interface in custom *.d.ts file

interface jquery {     chosen(options?:any):jquery; }     

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -