最基本的Angular app无法正常工作

时间:2015-03-15 16:28:51

标签: javascript angularjs

我使用yo angular来设置我的项目脚手架,但是将目录结构更改为模块化,现在我甚至无法使routeProvider工作。

app.js:

angular
  .module('feedsmanagerApp', [
    'ngRoute'
  ])
  .config(function ($routeProvider) {
      debugger;
    $routeProvider
      .when('/home', {
        templateUrl: 'scripts/feedsmanager/home.html',
        controller: 'HomeCtrl'
      })
      .otherwise({
        redirectTo: '/home'
      });
  });

脚本/ feedsmanager / home.js:

angular.module('feedsmanagerApp', [])
.controller('HomeCtrl', function ($scope) {
    alert('works');
});

的index.html

<body ng-app="feedsmanagerApp">
        <!--[if lt IE 7]>
          <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->

        <!-- Add your site or application content here -->
        <div ng-view></div>

        <!-- build:js(.) scripts/vendor.js -->
        <!-- bower:js -->
        <script src="bower_components/jquery/dist/jquery.js"></script>
        <script src="bower_components/angular/angular.js"></script>
        <script src="bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js"></script>
        <script src="bower_components/angular-animate/angular-animate.js"></script>
        <script src="bower_components/angular-cookies/angular-cookies.js"></script>
        <script src="bower_components/angular-resource/angular-resource.js"></script>
        <script src="bower_components/angular-route/angular-route.js"></script>
        <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
        <script src="bower_components/angular-touch/angular-touch.js"></script>
        <!-- endbower -->
        <!-- endbuild -->

        <!-- build:js({.tmp,app}) scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/feedsmanager/home.js"></script>
        <!-- endbuild -->
</body>

我已经把调试器语句,console.logs,任何东西试图让路径“/ home”工作,但我得到的只是一个空白页面而没有代码执行。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

你正在重新定义home.js中没有使用它的模块

修改

angular.module('feedsmanagerApp', []) // declare

angular.module('feedsmanagerApp')  // consume

答案 1 :(得分:0)

试试这个:

app.js:

angular
  .module('feedsmanagerApp', [
    'ngRoute', 'feedsmanagerAppHome'
  ])
  .config(function ($routeProvider) {
      debugger;
    $routeProvider
      .when('/home', {
        templateUrl: 'scripts/feedsmanager/home.html',
        controller: 'HomeCtrl'
      })
      .otherwise({
        redirectTo: '/home'
      });
  });

脚本/ feedsmanager / home.js:

angular.module('feedsmanagerAppHome', [])
.controller('HomeCtrl', function ($scope) {
    alert('works');
});