如何在AngularJs中调用第三方插件库
网络编程 2021-07-04 18:32www.168986.cn编程入门
在AngularJs中我们会不可避免的使用第三方库,这篇文章主要介绍了如何在AngularJs中调用第三方插件库,有兴趣的可以了解下
在AngularJs中我们会不可避免的使用第三方库,例如jquery插件库。我们不能散乱的在AngularJS中引入这些库,例如在controller中。那么应该怎么在Angular中使用第三方库呢?
如何使用?
很简单,给插件写一个directive。
在这里,我会使用一个简单的jquery插件Toolbar.js 的DEMO。
这是我们如何在jquery中创建一个tooltip的:
<!-- Click this to see a toolbar --> <div id="format-toolbar" class="settings-button"> <img src="http://paulkinzett.github./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: ''}"> <img src="http://paulkinzett.github./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中引用了第三方插件。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程