简体   繁体   English

Angular.js基本路由

[英]Angular.js basic routing

I have been following the tutorial: 我一直在关注本教程:

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

I have not done any rails integration yet, the question is specifically to angular. 我还没有进行任何Rails集成,这个问题是针对角度的。

When I do the hello worlds from the MainCtrl without using the router, everything works. 当我不使用路由器从MainCtrl打个招呼世界时,一切正常。 When I use the router, I cannot get the inline angular template to display in my html page. 使用路由器时,无法获取嵌入式角度模板以显示在html页面中。 Where is the error here? 错误在哪里?

app.js: 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: 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>

Your controller is recreating the module instead of referencing it. 您的控制器正在重新创建模块,而不是引用它。 Change it like so: 像这样更改它:

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

You're defining the 'flapperNews' module twice. 您要两次定义“ flapperNews”模块。 Remove the second angular.module('flapperNews', []). 删除第二个angular.module('flapperNews',[])。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM