我定义一个名为“greeting”的客户服务,但是不能从$inject.get(‘greeting’)获取实例.它会抛出这样的错误:未知的提供者:greetingProvider< - 问候.那是正确的方法吗?以下是代码:
var app = angular.module('myDI',[]); app.config(function($provide){ $provide.provider('greeting',function(){ this.$get = function(){ return function(name) { console.log("Hello," + name); }; }; }); }); var injector = angular.injector(); var greeting = injector.get('greeting'); greeting('Ford Prefect');
您需要从模块创建进样器.
var app = angular.module('myDI'," + name); }; }; }); }); var injector = angular.injector(['myDI','ng']); //Add this line var greeting = injector.get('greeting'); greeting('Ford Prefect'); var injector = angular.injector();
在这里尝试. FIDDLE