Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Using third party libraries without exposing a global variable

Tags:

angularjs

What is the best way to use a third party library in Angular without exposing a global variable?

For instance, if I were using underscore.js, I want to inject _ into only the controllers that use it.

angular.module('module').controller(function(_) {
  // _ is injected only into this scope
};

To get this effect, I've seen some people load underscore globally with a script tag, then create a service like this:

myModule.factory('_', function ($window) {
    return $window._;
});

However, this still pollutes the global scope with _.

Is there an 'Angular way' of registering and injecting third party libraries without causing this problem?

like image 314
Danny Nelson Avatar asked Apr 04 '14 21:04

Danny Nelson


People also ask

What is attrs in AngularJS?

scope is an AngularJS scope object. element is the jqLite-wrapped element that this directive matches. attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values. controller is the directive's required controller instance(s) or its own controller (if any).

What is$ scope AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

What is the use of scope apply in AngularJS?

Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the "AngularJS realm" (controllers, services, AngularJS event handlers). Scopes can be nested to limit access to the properties of application components while providing access to shared model properties.

What is the number of scopes allowed in an AngularJS application?

There are two types of scopes in Angular JS. How to use the Scope? When we make a controller in AngularJS, we will pass the $scope object as an argument. In AngularJS, it creates and injects a different $scope object into each controller in an application.


1 Answers

You can't do anything about how the third party libraries are written, except by forking them and creating your own version.

Third party libraries will almost always create such variables on the global scope, and you mention the two most frequently used ways of using them in an Angular module: by injecting $window and using them directly, or by creating a thin service which provides the object.

I would favor the latter in most cases because it makes the code in other modules simpler. Also, in my mind, the service approach makes the code in your controllers seem less 'magic' and more debuggable, as it is clear where the library, e.g. _ comes from. Variables appearing "out of thin air" is some of the problem with global variables to begin with.

The library will still "pollute" the global scope, unless you explicitly deletes them from the global scope, perhaps after fetching a pointer to them in a service, which then can pass it on. However, I can't see any motivation for doing so. Any other attempts, such as using an Angular service to actually download the third party script and evaluating it, somehow preventing it from reaching the global scope, seems vastly inefficient, over-engineered and slow (both computationally and with regards to when HTTP requests are fired).

It's otherwise a good idea not to create any more global variables than strictly necessary.

like image 72
Mikke Avatar answered Sep 28 '22 01:09

Mikke