Angularjs错误未知提供者

aga*_*yan 32 javascript angularjs angularjs-directive

我正在尝试在html页面中显示已配置的值authorversion角度值服务.版本代码显示正常但不是作者姓名这里是html代码

    <!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>
Run Code Online (Sandbox Code Playgroud)

这是指令......

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);
    };
  }]);
Run Code Online (Sandbox Code Playgroud)

这里是服务部分,其中authorversion值配置

    angular.module('myApp.services', []).
  value('version', '0.1')
  .value('author','JIM');
Run Code Online (Sandbox Code Playgroud)

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

Error: Unknown provider: authorProvider <- author <- appAuthorDirective
Run Code Online (Sandbox Code Playgroud)

bml*_*ite 49

确保将这些模块(myApp.servicesmyApp.directives)作为主应用程序模块的依赖项加载,如下所示:

angular.module('myApp', ['myApp.directives', 'myApp.services']);
Run Code Online (Sandbox Code Playgroud)

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


Juk*_*bom 31

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

如果在您的情况下这是正确的,您还应确保不要在多个文件中重新定义模块.

记得:

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

angular.module('ModuleName')       // gets you a pre-existing module.
Run Code Online (Sandbox Code Playgroud)

因此,如果要扩展现有模块,请记住在尝试获取模块时不要覆盖它.