Basic Angular:Uncaught SyntaxError:意外的令牌

时间:2016-03-31 08:46:05

标签: javascript angularjs angular-ui-router

我正在按照教程创建一个hackernews / reddit克隆,但由于某种原因,应用程序无法正常工作。

在我的浏览器控制台中,我收到以下错误:

UILabel

这里发生了什么?对不起,我不熟悉这个问题。

我在app.js

Uncaught SyntaxError: Unexpected token .               app.js:46 
Uncaught Error: [$injector:modulerr]                   angular.js:3857 http://errors.angularjs.org/1.2.19/$injector/modulerr?p0=flapperNews&p1=Err…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.19%2Fangular.min.js%3A18%3A139)

index.html看起来像这样:

angular.module('flapperNews', ['ui.router'])
.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');
}])
.factory('posts', [function(){
  var o = {
    posts: []
  };
  return o;
}])
.controller('MainCtrl', [
'$scope',
'posts',
function($scope, posts){
  $scope.posts = posts.posts;
  $scope.incrementUpvotes = function(post){
    post.upvotes += 1;
  }
  $scope.addPost = function(){
    if(!$scope.title || $scope.title === '') { return; }
    $scope.posts.push({
      title: $scope.title, 
      link: $scope.link,
      upvotes: 0
    });
    $scope.title = '';
    $scope.link = '';
  };
}]);
.controller('PostsCtrl', [
'$scope',
'$stateParams',
'$posts',
function($scope, $stateParams, posts) {
  $scope.posts.push({
    titile: $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.post = posts.posts[$stateParams.id];
}]);

当我删除;从第46行我在控制台中得到以下奇怪的东西。它似乎陷入了一个带有连续错误信息的循环中。

<html>
  <head>
    <title>My Angular App!</title>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

    <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.18/angular-ui-router.js"></script>
    <script src="app.js"></script>
    <style> .glyphicon-thumbs-up { cursor:pointer } </style>
  </head>
  <body ng-app="flapperNews">
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <ui-view></ui-view>
      </div>
    </div>

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

        <div class="page-header">
          <h1>Flapper 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>
        </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-templates" 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>
      </script>

  </body>
</html>

2 个答案:

答案 0 :(得分:5)

删除;控制器定义正上方的PostsCtrl

}]) //here
.controller('PostsCtrl'

;正在干扰控制器的链接。

答案 1 :(得分:1)

尝试在服务器,IIS或Apache下打开您的应用程序,不要只在浏览器中打开index.html