请问各路大神,怎么在angularjs项目中引入jquery插件啊

请问各路大神,怎么在angularjs项目中引入jquery插件啊

阅读 7.7k
5 个回答

简单粗暴的解决方法:
直接在index的html文件中通过script标签引入,然后在整个项目里随便用

项目分模块的话直接在需要用到jq插件的模块顶部 import js文件,或者直接在根模块 import 全局生效。

在最外层通过script标签直接引入,如果是打包模式的话,import进来就可以

我也在纠结这个问题,jquery引入没问题,但是使用插件的时候告诉我jquery无此属性(我是通过$(div).slideBox()这种方法调用的)

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {Component, ViewChild, ElementRef, AfterViewInit} from '@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);

类似这样引用吧,不过首先得

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

-----参考自https://stackoverflow.com/que...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进