如何使用jQuery与角2的javascript [英] How to use jQuery with javascript in angular 2
本文介绍了如何使用jQuery与角2的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import {Component, ElementRef, OnInit} from 'angular2/core';
declare var jQuery:any;
@Component({
selector: 'jquery-integration',
templateUrl: './components/jquery-integration/jquery-integration.html'
})
export class JqueryIntegration implements OnInit {
elementRef: ElementRef;
constructor(elementRef: ElementRef) {
this.elementRef = elementRef;
}
ngOnInit() {
jQuery(this.elementRef.nativeElement).draggable({
containment:'#draggable-parent'
});
}
}
jQuery的可以用在打字稿用Angular2像上面?我如何在Angular2使用jQuery JavaScript的?
Can jQuery be used with TypeScript in Angular2 like the above? How do I use jQuery with JavaScript in Angular2?
推荐答案
在我看来:如果你这样做是正确的,如果你把它最小的,你应该用去
In my opinion: if you do it right and if you keep it minimal, you should go with that.
- 在项目中安装jQuery分型:TSD中安装jQuery
- 加载与jQuery的脚本标记(或其他装载机...)
- 包装任何jQuery插件与角2指令
例如:
@Directive({
selector: "[sm-dropdown]"
})
export class SMDropdown {
constructor(el: ElementRef) {
jQuery(el.nativeElement).dropdown();
}
}
考虑这个Plunker:
Consider this Plunker:
https://plnkr.co/edit/MMNWGh?p=$p$ PVIEW
不要:
- 不得将其用于DOM操作,有角的方法...
- 请不要使用它,有角的方法...
- 请不要使用它的动画,ngAnimation来了......
对于AJAX调用
这篇关于如何使用jQuery与角2的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文