2013-03-06 84 views
30

我想显示在HTML代码page.Version角度值的服务配置的值authorversion显示正常,但不是作者的名字 下面是HTML代码Angularjs错误未知的提供

<!doctype html> 
<html lang="en" ng-app="myApp"> 
<head> 
    <meta charset="utf-8"> 
    <title>My AngularJS App</title> 
    <link rel="stylesheet" href="css/app.css"/> 
</head> 
<body> 
    <ul class="menu"> 
    <li><a href="#/view1">view1</a></li> 
    <li><a href="#/view2">view2</a></li> 
    </ul> 

    <div ng-view></div> 

    <div>Angular seed app: v<span app-version></span></div> 
    <div>Author is : <span app-author></span></div> 


    <script src="lib/angular/angular.js"></script> 
    <script src="js/app.js"></script> 
    <script src="js/services.js"></script> 
    <script src="js/controllers.js"></script> 
    <script src="js/filters.js"></script> 
    <script src="js/directives.js"></script> 
</body> 
</html> 

这里是指导...

angular.module('myApp.directives', []). 
    directive('appVersion', ['version', function(version) { 
    return function(scope, elm, attrs) { 
     elm.text(version); 
    }; 
    }]) 
    .directive('appAuthor', ['author', function(author) { 
    return function(scope, elm, attrs){ 
     elm.text(author); 
    }; 
    }]); 

,这里是哪里authorversion值配置

服务部
angular.module('myApp.services', []). 
    value('version', '0.1') 
    .value('author','JIM'); 

我得到开发者控制台中的错误是

Error: Unknown provider: authorProvider <- author <- appAuthorDirective 
+0

即使我被困在同样的问题,我解决了它,但我一直得到同样的问题。 请注意'函数getService(serviceName,调用者){if(cache.hasOwnProperty(serviceName))' 修复问题后**不要忘记做Ctrl + F5来清除浏览器缓存** – 2017-02-06 15:03:57

回答

48

确保您装载这些模块(myApp.servicesmyApp.directives)作为主应用程序模块的依赖性,像这样:

angular.module('myApp', ['myApp.directives', 'myApp.services']); 

plunker:http://plnkr.co/edit/wxuFx6qOMfbuwPq1HqeM?p=preview

+0

那么我是加载这些modules.but如果dat问题,那么版本号如何显示和作者不能 – agasthyan 2013-03-06 12:09:51

+0

你可以提供一个plunker或jsfiddle与问题的副本?这可能是一个错字或某种服务名称上的冲突。 (正如你所看到的,在我提供的那个上工作正常) – bmleite 2013-03-06 12:30:24

+1

以及我认为你是我失踪加载一些modules.Thanks dat帮助:) – agasthyan 2013-03-06 13:18:29

31

bmleite有关于包含模块的正确答案。

如果这在你的情况下是正确的,你也应该确保你没有重新定义多个文件中的模块。

记住:

angular.module('ModuleName', []) // creates a module. 

angular.module('ModuleName')  // gets you a pre-existing module. 

所以,如果要扩展一个现有的模块,切记不要试图把它拿来当覆盖。

+1

我不知道是这种情况。我定义了一个变量并将其设置为等于角度种子默认ctrls后的angular.module(...),并且遇到了同样的问题。为我省了很多头发。 – Kreychek 2014-07-04 17:44:58

+0

极好的提示搜索'“angular.module('..',”'在代码库中查看模块/'app'的创建位置,谢谢! – Dr1Ku 2015-01-07 15:06:07

+0

哇!这是天才! – ATrubka 2016-03-30 16:44:28

相关问题