如何在AngularJs中調(diào)用第三方插件庫
在AngularJs中我們會不可避免的使用第三方庫,例如jquery插件庫。我們不能散亂的在AngularJS中引入這些庫,例如在controller中。那么應(yīng)該怎么在Angular中使用第三方庫呢?
如何使用?
很簡單,給插件寫一個directive。
在這里,我會使用一個簡單的jquery插件Toolbar.js 的DEMO。
這是我們?nèi)绾卧趈query中創(chuàng)建一個tooltip的:
<!-- Click this to see a toolbar --> <div id="format-toolbar" class="settings-button"> <img src="http://paulkinzett.github.com/toolbar/img/icon-cog-small.png"> </div> <!-- Our tooltip style toolbar --> <div id="format-toolbar-options"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="icon-align-left"></i></a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="icon-align-center"></i></a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="icon-align-right"></i></a> </div>
<!-- Typical jQuery plugin invocation --> $('#format-toolbar').toolbar({ content: '#format-toolbar-options', position: 'left' });
在Angular中使用
在這里我們自定義一個元素屬性'toolbar-tip'--這使我們要寫的Angular directive。我們改寫下html:
<div id="format-toolbar1" class="settings-button" toolbar-tip="{content: '#format-toolbar-options', position: 'top'}"> <img src="http://paulkinzett.github.com/toolbar/img/icon-cog-small.png"> </div>
這里需要注意的一點是:我們把toolbar的options全部寫到了html中,這樣,我們就可以在任意地方使用相同的directive。
最終:
<script> var App = angular.module('Toolbar', []); App.directive('toolbarTip', function() { return { // Restrict it to be an attribute in this case restrict: 'A', // responsible for registering DOM listeners as well as updating the DOM link: function(scope, element, attrs) { $(element).toolbar(scope.$eval(attrs.toolbarTip)); } }; }); </script>
這樣就很簡單的在Angular中引用了第三方插件。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解基于Bootstrap+angular的一個豆瓣電影app
本篇文章主要介紹了基于Bootstrap+angular的一個豆瓣電影app ,非常具有實用價值,需要的朋友可以參考下2017-06-06掌握Queries設(shè)計模式優(yōu)化Angular應(yīng)用開發(fā)技巧
這篇文章主要介紹了掌握Queries設(shè)計模式優(yōu)化Angular應(yīng)用開發(fā)的技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10angularjs定時任務(wù)的設(shè)置與清除示例
本篇文章主要介紹了angularjs定時任務(wù)的設(shè)置與清除示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Angular?服務(wù)器端渲染錯誤消息localStorage?is?not?defined解決分析
這篇文章主要為大家介紹了Angular?服務(wù)器端渲染錯誤消息localStorage?is?not?defined解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07在 Angular-cli 中使用 simple-mock 實現(xiàn)前端開發(fā) API Mock 接口數(shù)據(jù)模擬功能的方法
這篇文章主要介紹了在 Angular-cli 中使用 simple-mock 實現(xiàn)前端開發(fā) API Mock 接口數(shù)據(jù)模擬功能的方法,需要的朋友可以參考下2018-11-11