欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解angular2采用自定義指令(Directive)方式加載jquery插件

 更新時(shí)間:2017年02月09日 16:01:49   作者:Liuyt  
本篇文章主要介紹了詳解angular2采用自定義指令(Directive)方式加載jquery插件 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

由于angular2興起不久,相關(guān)插件還是很少,所以有時(shí)候不得不用一些jquery插件來完成項(xiàng)目,

那么如何把jquery插件放到angular2中呢?采用自定義指令!

在上下文之前要引入jquery,這點(diǎn)不再描述

首先創(chuàng)建一個(gè)指令,采用@input方式,來獲取jquery插件所需要的參數(shù)。

在ngOnChanges時(shí),也就是參數(shù)通過@input傳入時(shí),初始化jquery插件,

初始化jquery插件需要獲取dom元素,所以我們引入ElementRef,用來獲取dom元素

這里需要講一下,jquery中回調(diào)函數(shù),如果直接使用this,回調(diào)是無法獲取angular的函數(shù)的

所以這里采用bind的形式,把this傳遞進(jìn)去。這樣在angular中的函數(shù)才會(huì)被正確調(diào)用。

以下為實(shí)現(xiàn)時(shí)間插件的代碼:

import { Directive, Output, Input, ElementRef, EventEmitter } from '@angular/core';

// 引入jquery.daterangepicker插件相關(guān)JS和css,Css打包時(shí)需要打包成單個(gè)文件,或者直接在html單獨(dú)引用
// 如何單獨(dú)打包請(qǐng)看下節(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ā)生更改時(shí)
  * @private
  */
 ngOnChanges() {
  $(this._elementRef.nativeElement).daterangepicker(this.dateRangePickerOptions, this.dateCallback.bind(this));
 }

 /**
  * 時(shí)間發(fā)生更改時(shí)使用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 {

 /**
  * 當(dāng)前選中的時(shí)間
  */
 public date: any

 /**
  * jquery時(shí)間插件參數(shù)
  */
 private option: Object = {
  locale: {
   format: "YYYY-MM-DD",
   separator: " 至 ",
   applyLabel: "確定",
   cancelLabel: '取消',
   fromLabel: '起始時(shí)間',
   toLabel: '結(jié)束時(shí)間',
   customRangeLabel: '自定義',
   daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
   monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
    '七月', '八月', '九月', '十月', '十一月', '十二月'],
   firstDay: 1
  },
 };

 constructor() {
 }

 /**
  * emit回調(diào)事件,獲取選中時(shí)間
  * @param date
  */
 dateSelected(date) {
  this.date = date;
 }
}

另外注意,css需要另外單獨(dú)打包,或html單獨(dú)引用,如何打包c(diǎn)ss,請(qǐng)看最后,我這里是用webpack打包的

// 采用ExtractTextPlugin單獨(dú)對(duì)jquery插件進(jìn)行css打包
loaders: [{
    test: /daterangepicker\.css$/,
    loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
   }]

plugins: [
     new ExtractTextPlugin('[name].css', {
      allChunks: true
     })]

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • angular6 填坑之sdk的方法

    angular6 填坑之sdk的方法

    這篇文章主要介紹了angular6 填坑之sdk的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • 解析AngularJS中g(shù)et請(qǐng)求URL出現(xiàn)的跨域問題

    解析AngularJS中g(shù)et請(qǐng)求URL出現(xiàn)的跨域問題

    本文主要介紹了AngularJS中g(shù)et請(qǐng)求URL出現(xiàn)跨域問題。需要的朋友可以參考下
    2016-12-12
  • AngularJS實(shí)踐之使用ng-repeat中$index的注意點(diǎn)

    AngularJS實(shí)踐之使用ng-repeat中$index的注意點(diǎn)

    最近通過客戶的投訴主要到在ng-repeat中使用了$index引發(fā)的一個(gè)bug,下面一起來看看這個(gè)錯(cuò)誤是如何引發(fā)的, 以及如何避免這種bug產(chǎn)生,然后說說我們從中得到的經(jīng)驗(yàn)和教訓(xùn)。有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • 淺析AngularJS Filter用法

    淺析AngularJS Filter用法

    系統(tǒng)的學(xué)習(xí)了一下angularjs,發(fā)現(xiàn)angularjs的有些思想根php的模塊smarty很像,例如數(shù)據(jù)綁定,filter。如果對(duì)smarty比較熟悉的話,學(xué)習(xí)angularjs會(huì)比較容易一點(diǎn),這篇文章給大家介紹angularjs filter用法詳解,感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • angular.js指令中的controller、compile與link函數(shù)的不同之處

    angular.js指令中的controller、compile與link函數(shù)的不同之處

    最近一位大神問了我angular.js指令中的controller、compile與link函數(shù)的不同,想了想居然回答不出來,所以必須要深入的探究下,下面這篇文章主要介紹了關(guān)于angular.js指令中的controller、compile與link函數(shù)的不同之處,需要的朋友可以參考下。
    2017-05-05
  • angularjs實(shí)現(xiàn)猜數(shù)字大小功能

    angularjs實(shí)現(xiàn)猜數(shù)字大小功能

    這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)猜數(shù)字大小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Angular中自定義Debounce Click指令防止重復(fù)點(diǎn)擊

    Angular中自定義Debounce Click指令防止重復(fù)點(diǎn)擊

    本篇文章主要介紹了Angular中自定義Debounce Click指令詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 淺析angularJS中的ui-router和ng-grid模塊

    淺析angularJS中的ui-router和ng-grid模塊

    下面小編就為大家?guī)硪黄獪\析angularJS中的ui-router和ng-grid模塊。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • AngularJS基礎(chǔ) ng-keydown 指令簡(jiǎn)單示例

    AngularJS基礎(chǔ) ng-keydown 指令簡(jiǎn)單示例

    本文主要介紹AngularJS ng-keydown 指令,在這里幫大家整理了ng-keydown 指令的基礎(chǔ)資料,并附有代碼,有需要的朋友可以參考下
    2016-08-08
  • 詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程

    詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程

    本篇文章主要介紹了詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程 ,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08

最新評(píng)論