詳解angular2采用自定義指令(Directive)方式加載jquery插件
由于angular2興起不久,相關(guān)插件還是很少,所以有時候不得不用一些jquery插件來完成項目,
那么如何把jquery插件放到angular2中呢?采用自定義指令!
在上下文之前要引入jquery,這點不再描述
首先創(chuàng)建一個指令,采用@input方式,來獲取jquery插件所需要的參數(shù)。
在ngOnChanges時,也就是參數(shù)通過@input傳入時,初始化jquery插件,
初始化jquery插件需要獲取dom元素,所以我們引入ElementRef,用來獲取dom元素
這里需要講一下,jquery中回調(diào)函數(shù),如果直接使用this,回調(diào)是無法獲取angular的函數(shù)的
所以這里采用bind的形式,把this傳遞進去。這樣在angular中的函數(shù)才會被正確調(diào)用。
以下為實現(xiàn)時間插件的代碼:
import { Directive, Output, Input, ElementRef, EventEmitter } from '@angular/core';
// 引入jquery.daterangepicker插件相關(guān)JS和css,Css打包時需要打包成單個文件,或者直接在html單獨引用
// 如何單獨打包請看下節(jié)代碼
require('../../../../assets/lib/bootstrap-daterangepicker-master/daterangepicker.js');
require('../../../../assets/lib/bootstrap-daterangepicker-master/daterangepicker.css');
// 自定義指令
@Directive({
selector: '[dateRangePicker]',
})
export class DateRangePicker {
/**
* jquery.daterangepicker插件所需的參數(shù)
* 參數(shù):http://www.daterangepicker.com/#options
*/
@Input() public dateRangePickerOptions: IJQueryDateRangePicker;
// 選中事件
@Output() selected: any = new EventEmitter();
/**
* 初始化
* @param _elementRef
*/
constructor(private _elementRef: ElementRef) {
}
/**
* 屬性發(fā)生更改時
* @private
*/
ngOnChanges() {
$(this._elementRef.nativeElement).daterangepicker(this.dateRangePickerOptions, this.dateCallback.bind(this));
}
/**
* 時間發(fā)生更改時使用emit傳遞事件
* @private
*/
dateCallback(start, end) {
let format = "YYYY-MM-DD";
if (this.dateRangePickerOptions.locale.format) {
format = this.dateRangePickerOptions.locale.format;
}
let date = {
startDate: start.format(format),
endDate: end.format(format),
}
this.selected.emit(date);
}
}
import { Component } from '@angular/core';
import { DateRangePicker } from '../../theme/directives';
@Component({
selector: 'dashboard',
template: `
<div class="form-group">
<label for="startDate">{date.startDate}</label>
<input
dateRangePicker
[dateRangePickerOptions]="option"
(selected)="dateSelected($event)"
type="text"
class="form-control">
</div>
`,
directives: [DateRangePicker]
})
export class Dashboard {
/**
* 當前選中的時間
*/
public date: any
/**
* jquery時間插件參數(shù)
*/
private option: Object = {
locale: {
format: "YYYY-MM-DD",
separator: " 至 ",
applyLabel: "確定",
cancelLabel: '取消',
fromLabel: '起始時間',
toLabel: '結(jié)束時間',
customRangeLabel: '自定義',
daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
'七月', '八月', '九月', '十月', '十一月', '十二月'],
firstDay: 1
},
};
constructor() {
}
/**
* emit回調(diào)事件,獲取選中時間
* @param date
*/
dateSelected(date) {
this.date = date;
}
}
另外注意,css需要另外單獨打包,或html單獨引用,如何打包css,請看最后,我這里是用webpack打包的
// 采用ExtractTextPlugin單獨對jquery插件進行css打包
loaders: [{
test: /daterangepicker\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
}]
plugins: [
new ExtractTextPlugin('[name].css', {
allChunks: true
})]
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解析AngularJS中g(shù)et請求URL出現(xiàn)的跨域問題
本文主要介紹了AngularJS中g(shù)et請求URL出現(xiàn)跨域問題。需要的朋友可以參考下2016-12-12
AngularJS實踐之使用ng-repeat中$index的注意點
最近通過客戶的投訴主要到在ng-repeat中使用了$index引發(fā)的一個bug,下面一起來看看這個錯誤是如何引發(fā)的, 以及如何避免這種bug產(chǎn)生,然后說說我們從中得到的經(jīng)驗和教訓。有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
angular.js指令中的controller、compile與link函數(shù)的不同之處
最近一位大神問了我angular.js指令中的controller、compile與link函數(shù)的不同,想了想居然回答不出來,所以必須要深入的探究下,下面這篇文章主要介紹了關(guān)于angular.js指令中的controller、compile與link函數(shù)的不同之處,需要的朋友可以參考下。2017-05-05
Angular中自定義Debounce Click指令防止重復點擊
本篇文章主要介紹了Angular中自定義Debounce Click指令詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
淺析angularJS中的ui-router和ng-grid模塊
下面小編就為大家?guī)硪黄獪\析angularJS中的ui-router和ng-grid模塊。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
AngularJS基礎(chǔ) ng-keydown 指令簡單示例
本文主要介紹AngularJS ng-keydown 指令,在這里幫大家整理了ng-keydown 指令的基礎(chǔ)資料,并附有代碼,有需要的朋友可以參考下2016-08-08
詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程
本篇文章主要介紹了詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程 ,具有一定的參考價值,有興趣的可以了解一下2017-08-08

