2

最初我的 controller.js 看起来像这样

    function MyCtrl1() {}
MyCtrl1.$inject = [];


function MyCtrl2() {
}
MyCtrl2.$inject = [];

像这样的html代码

    <!doctype html>
<html lang="en" ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>My AngularJS App</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
  <script src="http://angular-ui.github.com/bootstrap/ui-bootstrap-tpls-0.2.0.js"></script>
  <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="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>

这是 service.js 代码

    angular.module('myApp.services', []).
  value('version', '0.1')
  .value('author','Jay');

和directive.js 代码

    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);
    };
  }]);

上面的代码工作得很好,并在 service.js 中显示version和配置author

当我修改我的 controller.js 以包含一个新的控制器时,它会停止工作,并且不会显示版本或作者。修改后的控制器代码如下

    function MyCtrl1() {}
MyCtrl1.$inject = [];


function MyCtrl2() {
}
MyCtrl2.$inject = [];

angular.module('myApp', ['ui.bootstrap']);
var TabsDemoCtrl = function ($scope) {
  $scope.panes = [
    { title:"Dynamic Title 1", content:"Dynamic content 1" },
    { title:"Dynamic Title 2", content:"Dynamic content 2" }
  ];
};
TabsDemoCtrl.$inject = ['$scope'];

任何指示为什么这件事不起作用。

4

1 回答 1

4

看起来您的问题是 myApp 在这里重新声明:

angular.module('myApp', ['ui.bootstrap']);

当我有时,我能够重现您的问题

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

切换到

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

让一切再次正常工作。

于 2013-03-07T17:48:55.717 回答