3

我正在尝试创建一个简单的示例,从 AngularJS 的服务解决方案中获取 promise/defer 对象:

var $q;

function init() {

    var $injector = window.angular.injector();
    console.log($injector);
    $injector.invoke(["$q", function (_$q) {
        console.log($q);
        $q = _$q;
    }]);

}

init();

但这会导致:

错误:未知提供者:$qProvider <- $q [Break On This Error]
throw Error("未知提供者:" + path.join(' <- '));

我可能错过了什么?

4

2 回答 2

13

您必须像这样添加提供程序所在的模块:

var $injector = window.angular.injector(['ng']);

然后它将起作用!

编辑:关于“ng”模块,文档明确表示必须明确添加。从角度喷射器文档

modules – {Array.<string|Function>} – A list of module functions or their aliases.
See angular.module. The ng module must be explicitly added.
于 2013-05-03T13:15:54.327 回答
0

如果您不在 angular.js 模块生命周期之外,请使用https://github.com/kriskowal/q,我已经走上了这条路,它不会很好地工作,$q 不打算在引导程序之外使用模块(需要 $rootScope)。

于 2013-05-03T14:25:14.283 回答