Can I use AngularJS $ q outside the Angular component?

I am writing a browser application and I have a file that creates an object and initializes it. The application is written in AngularJS, but this file is simple Javascript, outside the Angular ecosystem.

I want to use promises in this file, but since Angular contains Q , I would rather just use this than contribute another library.

I also use RequireJS.

So, is there a way to use $ q in an Angular file?

+5
source share
1 answer

You can do this using the angular.injector () method, which returns a $ injector , which can be entered with the necessary dependencies (for example, $http , $q ) through its invoke() method.

Demo

Something like that:

 angular.injector(['ng']).invoke(['$q', function($q) { $q.when('hello world').then(function(message) { window.alert(message); }); }]); 

Please note that the array passed to angular.injector() is a list of modules, I included the ng module, because it is where AngularJS core dependencies are located.

+9
source

Source: https://habr.com/ru/post/1202630/


All Articles