添加新控制器后,Angularjs app无法正常工作

时间:2013-03-07 16:39:57

标签: javascript angularjs angularjs-directive angular-ui

最初我的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中配置versionauthor

当我修改我的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'];

任何指示为什么这个东西不起作用。

1 个答案:

答案 0 :(得分: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']);

让一切恢复正常。