AngularJS - $ stateProvider - 如何定义许多状态

时间:2015-07-18 07:17:34

标签: angularjs

我一直致力于本教程:Thinkster MEAN tutorial

在我完成路由并创建了“帖子页面”之前,它运行得很顺利。我忘了测试我在过程中编写的代码,当我测试它时 - 没有任何东西显示:-(我一直在努力找出可能出错的东西,但我找不到任何东西,那个会使HTML不显示任何内容。

我有这两个文件。请帮助我理解,为什么没有任何表现。

的index.html:

<html>

<head>
  <title>Cortrium News</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.3.10/angular.min.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>

  <script src="app.js"></script>
  <style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>

<body ng-app="flapperNews" ng-controller="MainCtrl">
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <ui-view></ui-view> 
    </div>
  </div>

  <script type="text/ng-template" id="/home.html">

    <div class="page-header">
        <h1>Cortrium News</h1>
      </div>

      <div ng-repeat="post in posts | orderBy:'-upvotes'">

        <span class="glyphicon glyphicon-thumbs-up"
          ng-click="incrementUpvotes(post)"></span>
        {{post.upvotes}}
        <span style="font-size:20px; margin-left:10px;">
          <a ng-show="post.link" href="{{post.link}}">
            {{post.title}}
          </a>
          <span ng-hide="post.link">
            {{post.title}}
            </span>
          </span>

        <span>
          <a href="#/posts/{{$index}}">Comments</a>
        </span>
      </div>

      <form ng-submit="addPost()"
        style="margin-top:30px;">
        <h3>Add a new post</h3>

        <div class="form-group">
          <input type="text"
            class="form-control"
            placeholder="Title"
            ng-model="title"></input>
        </div>
        <div class="form-group">
          <input type="text"
          class="form-control"
          placeholder="Link"
          ng-model="link"></input>
        </div>
        <button type="submit" class="btn btn-primary">Post</button>
      </form>
</script>

<script type="text/ng-template" id="/posts.html">
  <div class="page-header">
    <h3>
      <a ng-show="post.link" href="{{post.link}}">
        {{post.title}}
      </a>
      <span ng-hide="post.link">
        {{post.title}}
      </span>
    </h3>
  </div>

  <div ng-repeat="comment in post.comments | orderBy:'-upvotes'">
    <span class="glyphicon glyphicon-thumbs-up"
      ng-click="incrementUpvotes(comment)"></span>
    {{comment.upvotes}} - by {{comment.author}}
    <span style="font-size:20px; margin-left:10px;">
      {{comment.body}}
    </span>
  </div>

    <form ng-submit="addComment()"
    style="margin-top:30px;">
    <h3>Add a new comment</h3>

    <div class="form-group">
      <input type="text"
      class="form-control"
      placeholder="Comment"
      ng-model="body"></input>
    </div>
    <button type="submit" class="btn btn-primary">Post</button>
  </form>

</script>

</body>
</html>

app.js:

var app = angular.module('flapperNews', ['ui.router']);

app.config([
'$stateProvider',
'$urlRouterProvider',

function($stateProvider, $urlRouterProvider) {

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

    .state('posts', 
    {
        url: '/posts/{id}',
        templateUrl: '/posts.html',
        controller: 'PostsCtrl'
    });

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

app.factory('posts', [function(){

    var o = {
        posts: []
    };
    return o;

}])

app.controller('MainCtrl', [
'$scope',
'posts',
function($scope, posts){
  $scope.test = 'Hello world!';

$scope.posts = posts.posts;

$scope.addPost = function (){
    if ($scope.title === '') {return};
    $scope.posts.push({
    title: $scope.title,
    link: $scope.link,
     upvotes: 0,
    comments: [
        {author: 'Joe', body: 'Cool post!', upvotes: 0},
        {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
  ]
});
    $scope.title = '';
    $scope.link = '';
}

$scope.incrementUpvotes = function(post){
    post.upvotes += 1;
}
}])
.controller('PostsCtrl', [
'$scope',
'$stateParams',
'posts',
function($scope, $stateParams, posts){
$scope.post = posts.posts[$stateParams.id];

$scope.addComment = function (){
    if ($scope.body === '') {return};
    $scope.posts.comments.push({
    body: $scope.body,
    author: 'user',
     upvotes: 0,
    });

    $scope.body = '';

}

}]);

1 个答案:

答案 0 :(得分:1)

分区有多个分号:

 $stateProvider
  .state('home', {
    url: '/home',
    templateUrl: '/home.html',
    controller: 'MainCtrl'
  }) // ; remove semicolon as you want to chain many methods to $stateProvider
  .state('posts', {
    url: '/posts/{id}',
    templateUrl: '/posts.html',
    controller: 'PostsCtrl'
  });

var app = angular.module('flapperNews', ['ui.router']);

app.config([
  '$stateProvider',
  '$urlRouterProvider',

  function($stateProvider, $urlRouterProvider) {

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

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

app.factory('posts', [
  function() {

    var o = {
      posts: []
    };
    return o;

  }
])

app.controller('MainCtrl', [
    '$scope',
    'posts',
    function($scope, posts) {
      $scope.test = 'Hello world!';

      $scope.posts = posts.posts;

      $scope.addPost = function() {
        if ($scope.title === '') {
          return
        };
        $scope.posts.push({
          title: $scope.title,
          link: $scope.link,
          upvotes: 0,
          comments: [{
            author: 'Joe',
            body: 'Cool post!',
            upvotes: 0
          }, {
            author: 'Bob',
            body: 'Great idea but everything is wrong!',
            upvotes: 0
          }]
        });
        $scope.title = '';
        $scope.link = '';
      }

      $scope.incrementUpvotes = function(post) {
        post.upvotes += 1;
      }
    }
  ])
  .controller('PostsCtrl', [
    '$scope',
    '$stateParams',
    'posts',
    function($scope, $stateParams, posts) {
      $scope.post = posts.posts[$stateParams.id];

      $scope.addComment = function() {
        if ($scope.body === '') {
          return
        };
        $scope.posts.comments.push({
          body: $scope.body,
          author: 'user',
          upvotes: 0,
        });

        $scope.body = '';

      }

    }
  ]);
<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.3.10/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>


<div ng-app="flapperNews" ng-controller="MainCtrl">
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <ui-view></ui-view>
    </div>
  </div>

  <script type="text/ng-template" id="/home.html">

    <div class="page-header">
      <h1>Cortrium News</h1>
    </div>

    <div ng-repeat="post in posts | orderBy:'-upvotes'">

      <span class="glyphicon glyphicon-thumbs-up" ng-click="incrementUpvotes(post)"></span>
      {{post.upvotes}}
      <span style="font-size:20px; margin-left:10px;">
          <a ng-show="post.link" href="{{post.link}}">
            {{post.title}}
          </a>
          <span ng-hide="post.link">
            {{post.title}}
            </span>
      </span>

      <span>
          <a href="#/posts/{{$index}}">Comments</a>
        </span>
    </div>

    <form ng-submit="addPost()" style="margin-top:30px;">
      <h3>Add a new post</h3>

      <div class="form-group">
        <input type="text" class="form-control" placeholder="Title" ng-model="title"></input>
      </div>
      <div class="form-group">
        <input type="text" class="form-control" placeholder="Link" ng-model="link"></input>
      </div>
      <button type="submit" class="btn btn-primary">Post</button>
    </form>
  </script>

  <script type="text/ng-template" id="/posts.html">
    <div class="page-header">
      <h3>
      <a ng-show="post.link" href="{{post.link}}">
        {{post.title}}
      </a>
      <span ng-hide="post.link">
        {{post.title}}
      </span>
    </h3>
    </div>

    <div ng-repeat="comment in post.comments | orderBy:'-upvotes'">
      <span class="glyphicon glyphicon-thumbs-up" ng-click="incrementUpvotes(comment)"></span>
      {{comment.upvotes}} - by {{comment.author}}
      <span style="font-size:20px; margin-left:10px;">
      {{comment.body}}
    </span>
    </div>

    <form ng-submit="addComment()" style="margin-top:30px;">
      <h3>Add a new comment</h3>

      <div class="form-group">
        <input type="text" class="form-control" placeholder="Comment" ng-model="body"></input>
      </div>
      <button type="submit" class="btn btn-primary">Post</button>
    </form>

  </script>

</div>