Angular.js基本路由

时间:2016-09-12 20:19:12

标签: javascript ruby-on-rails angularjs

我一直在关注教程:

https://thinkster.io/angular-rails#angular-routing

我还没有完成任何导轨集成,问题特别针对角度。

当我在不使用路由器的情况下从MainCtrl执行hello世界时,一切正常。当我使用路由器时,我无法在我的html页面中显示内联角度模板。这里的错误在哪里?

app.js:

angular.module('flapperNews', ['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: '/home.html',
      controller: 'MainCtrl'
    });

  $urlRouterProvider.otherwise('home');
}])

angular.module('flapperNews', [])
.controller('MainCtrl', [
'$scope',
function($scope){
  $scope.test = 'Hello world';
}]);

的index.html:

<html>

  <head>
    <title>My Angular App</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-app="flapperNews">

    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <ui-view></ui-view> <!-- this is supposed to display the template below but it shows nothing -->
      </div>
    </div>

    <script type="text/ng-template" id="/home.html">
      <div class="page-header">
        <h1>Flapper News</h1>
      </div>
    </script>
  </body>

</html>

2 个答案:

答案 0 :(得分:3)

您的控制器正在重新创建模块而不是引用它。像这样改变:

angular.module('flapperNews')
.controller('MainCtrl', [
'$scope',
function($scope){
  $scope.test = 'Hello world';
}]);

答案 1 :(得分:0)

您正在定义&#39; flapperNews&#39;模块两次。删除第二个angular.module(&#39; flapperNews&#39;,[])。