AngularJs console.log “$q is not defined”

佐手、 提交于 2019-12-04 02:33:58

Promises are not deprecated. In fact they're gaining quite a lot of momentum lately and are included in the next version of JavaScript.

Let's look at what they say:

This breeze.angular.q library has been deprecated. It is superseded by the Breeze Angular Service which more cleanly configures breeze for Angular development.

The Breeze Angular Service tells Breeze to use Angular's $q for promises and to use Angular's $http for ajax calls.

What they say is that breeze uses Angular's own promises for promises rather than its own breeze.angular.q which uses Q promises which are more able but also much heavier than $q promises which Angular uses. This is simply an API change.

Inside Angular code, you can obtain $q using dependency injection - for example with the simple syntax:

myApp.controller("MyCtrl",function($q){
    //$q is available here
});

Alternatively, if you want to use it independently you can use service location and obtain $q directly from an injector, but that's rarely the case. (If you want an example - let me know, I'd just rather not include code that's usually indicative of bad practice).

# in your console, try following code
$injector = angular.injector(['ng']);
q = $injector.get('$q');
deferred = q.defer();
# then do whatever you want
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!