SoFunction
Updated on 2025-04-08

How to call a third-party plugin library in AngularJs

In AngularJs, we will inevitably use third-party libraries, such as the jquery plug-in library. We cannot introduce these libraries in AngularJS in a mess, such as in controllers. So how should we use third-party libraries in Angular?

How to use it?

It's very simple, write a directive to the plugin.

Here I'll use a simple jquery plugin for DEMO.

Here is how we create a tooltip in jquery:

<!-- Click this to see a toolbar --> 
<div  class="settings-button"> 
  <img src="/toolbar/img/"> 
</div> 
  
<!-- Our tooltip style toolbar --> 
<div > 
  <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' 
}); 

Use in Angular

Here we customize an element property 'toolbar-tip' - which makes the Angular directive we want to write. Let's rewrite the html:

<div  class="settings-button" toolbar-tip="{content: '#format-toolbar-options', position: 'top'}"> 
  <img src="/toolbar/img/"> 
</div> 

One thing to note here is that we write all the toolbar options into html, so that we can use the same directive anywhere.
final:

<script> 
var App = ('Toolbar', []); 
  
('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()); 
    } 
  }; 
}); 
</script> 

This makes it easy to reference third-party plugins in Angular.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.