angular2採用自訂指令(Directive)方式載入jquery外掛程式

來源:互聯網
上載者:User

標籤:

由於angular2興起不久,相關外掛程式還是很少,所以有時候不得不用一些jquery外掛程式來完成項目,

那麼如何把jquery外掛程式放到angular2中呢?採用自訂指令!

 

在上下文之前要引入jquery,這點不再描述

首先建立一個指令,採用@input方式,來擷取jquery外掛程式所需要的參數。

在ngOnChanges時,也就是參數通過@input傳入時,初始化jquery外掛程式,

初始化jquery外掛程式需要擷取dom元素,所以我們引入ElementRef,用來擷取dom元素

這裡需要講一下,jquery中回呼函數,如果直接使用this,回調是無法擷取angular的函數的

所以這裡採用bind的形式,把this傳遞進去。這樣在angular中的函數才會被正確調用。

 

以下為實現時間外掛程式的代碼:

import { Directive, Output, Input, ElementRef, EventEmitter } from ‘@angular/core‘;// 引入jquery.daterangepicker外掛程式相關JS和css,Css打包時需要打包成單個檔案,或者直接在html單獨引用
// 如何單獨打包請看下節代碼
require(‘../../../../assets/lib/bootstrap-daterangepicker-master/daterangepicker.js‘);require(‘../../../../assets/lib/bootstrap-daterangepicker-master/daterangepicker.css‘);// 自訂指令@Directive({ selector: ‘[dateRangePicker]‘,})export class DateRangePicker { /** * jquery.daterangepicker外掛程式所需的參數 * 參數:http://www.daterangepicker.com/#options */ @Input() public dateRangePickerOptions: IJQueryDateRangePicker; // 選中事件 @Output() selected: any = new EventEmitter(); /** * 初始化 * @param _elementRef */ constructor(private _elementRef: ElementRef) { } /** * 屬性發生更改時 * @private */ ngOnChanges() { $(this._elementRef.nativeElement).daterangepicker(this.dateRangePickerOptions, this.dateCallback.bind(this)); } /** * 時間發生更改時使用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時間外掛程式參數   */  private option: Object = {    locale: {      format: "YYYY-MM-DD",      separator: " 至 ",      applyLabel: "確定",      cancelLabel: ‘取消‘,      fromLabel: ‘起始時間‘,      toLabel: ‘結束時間‘,      customRangeLabel: ‘自訂‘,      daysOfWeek: [‘日‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘],      monthNames: [‘一月‘, ‘二月‘, ‘三月‘, ‘四月‘, ‘五月‘, ‘六月‘,        ‘七月‘, ‘八月‘, ‘九月‘, ‘十月‘, ‘十一月‘, ‘十二月‘],      firstDay: 1    },  };  constructor() {  }  /**   * emit回調事件,擷取選中時間   * @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         })]

 

angular2採用自訂指令(Directive)方式載入jquery外掛程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.