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.
source share