admin管理员组文章数量:1379611
I am writing a browser app, and I have a file that creates an object and initializes it. The app is written in AngularJS, but the file in question is plain Javascript, outside the Angular ecosystem.
I want to use promises in that file, but since Angular contains an an implementation of Q, I'd rather just use that than bring in another library.
I am also using RequireJS.
So, is there a way to use $q in a non-Angular file?
I am writing a browser app, and I have a file that creates an object and initializes it. The app is written in AngularJS, but the file in question is plain Javascript, outside the Angular ecosystem.
I want to use promises in that file, but since Angular contains an an implementation of Q, I'd rather just use that than bring in another library.
I am also using RequireJS.
So, is there a way to use $q in a non-Angular file?
Share Improve this question asked Sep 15, 2014 at 23:09 FrontierPsychoFrontierPsycho 7439 silver badges25 bronze badges 1- i don't see why not- just pass it at the appropriate time. – Daniel A. White Commented Sep 15, 2014 at 23:15
1 Answer
Reset to default 9You can do this by using the angular.injector() method which returns an $injector function that can be injected with dependencies that you need (e.g. $http
, $q
) through its invoke()
method.
DEMO
Something like this:
angular.injector(['ng']).invoke(['$q', function($q) {
$q.when('hello world').then(function(message) {
window.alert(message);
});
}]);
Note that the array passed to angular.injector()
is a list of modules, I included the ng
module because that is where the AngularJS core dependencies are located.
本文标签: javascriptCan I use AngularJS39s q outside an Angular componentStack Overflow
版权声明:本文标题:javascript - Can I use AngularJS's $q outside an Angular component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744497132a2609096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论