2015-11-11 78 views
0

我试着去了解怎么样了供应商的作品,我让基于angularjs文档中的一个测试,我写了一个简单的供应商:

(function(window, angular, undefined){"use strict"; 
    function MyProviderExample(foo) 
    { 
     this.testdrive = function() 
     { 
     console.log(foo); 
     } 
     console.log("init"); 
    } 
    angular.module('app',[]) 
    .provider('$myProvider',function(){ 
     var foo = "bar"; 
     this.$get = function() 
     { 
     return new MyProviderExample(foo); 
     } 
     console.log("ey...."); 

    }).config(function($myProvider){ 
     console.log("wut"); 
     $myProvider.foo = "foo"; 
    }); 
})(window, window.angular); 

当我始终运行的代码返回

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: 
Error: [$injector:unpr] Unknown provider: $myProvider 

我试图理解失败,但我看不到我的错,如果有人能帮助我欣赏

回答

0

我认为你需要删除function($myProvider).config部分。就像这样:

angular.module('app', []) 
    .provider('$myProvider', function() {    
     this.$get = function() { 
      // -- 
     } 
     console.log("loaded $myProvider"); 
    }) 
    .config(function(){ 
     console.log("loaded config"); 
    }) 
    .controller('Main', 
     function main() { 
      console.log('loaded mycontroller') 
     }); 

什么是你想用$ myProvider.foo = "foo";办?

相关问题