【问题标题】:Thinkster MEAN Stack tutorialThinkster MEAN Stack 教程
【发布时间】:2016-11-03 21:27:33
【问题描述】:

我正在做关于 MEAN 堆栈和 Flapper News 的 thinkster.io 教程。 https://thinkster.io/mean-stack-tutorial/ 我在 Beginning Node 部分之前停了下来。我的代码一直工作到本教程前半部分的后半部分。我希望有人能帮助我找出问题所在,因为我只是 MEAN 的初学者。

我的index.html

<html>
<head>
    <title>Flapper 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">
    <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>Flapper News</h1>
    </div>

    <div ng-repeat="post in posts | orderBy:'-upvotes'">
        <span class="glyphicon glyphicon-thumbs-up"
            ng-click="upvote(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="theTitle"></input>
        </div>
        <div class="form-group">
            <input type="text"
                class="form-control"
                placeholder="Link"
                ng-model="theLink"></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>
</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.posts = posts.posts;
    $scope.addPost = function(){
        if(!$scope.theTitle || $scope.theTitle === '') { return; }
        $scope.posts.push({
            title: $scope.theTitle,
            link: $scope.theLink,
            upvotes: 0
            comments: [
                {author: 'Joe', body: 'Cool post!', upvotes: 0},
                {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
            ]
        });
        $scope.theTitle = '';
        $scope.theLink = '';
    }
    $scope.upvote = function(post){
        post.upvotes++;
    }
}]);

app.controller('PostsCtrl', [
'$scope',
'$stateParams',
'posts',
function($scope, $stateParams, posts){
    $scope.post = posts.posts[$stateParams.id];
    $scope.addComment = function(){
        if($scope.body === '') { return; }
        $scope.post.comments.push({
            body: $scope.body,
            author: 'user',
            upvotes: 0
        });
        $scope.body = '';
    };
}]);

我目前通过在 chromium 中打开 index.html 来运行这个骨架。

【问题讨论】:

  • 该指南也有问题。你得到什么错误?
  • 您的实际问题是什么?发布大量代码并声明它不起作用并不能完全回答这个问题......
  • 当你说你在chromium中打开了index.html,你的意思是你去了c:/mywebiste/index.html,还是http://localhost/index.html?一个会工作,另一个可能不会工作。
  • @Claies 一般来说,你是对的。但是,如果没有本地服务器实例,这个可以正常工作。 ;)
  • @kiswa 这就是我说可能的原因,尽管我没有花很多时间分析代码以查看它在做什么或哪里可能存在语法错误....

标签: javascript angularjs


【解决方案1】:

app.js 中有错别字,会阻止它工作。以后,请使用 F12 键打开浏览器的开发人员工具,然后按照错误提供的链接查看要更改的行。

具体来说,有几个地方多了一个;,而缺少,s。

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.posts = posts.posts;
    $scope.addPost = function(){
        if(!$scope.theTitle || $scope.theTitle === '') { return; }
        $scope.posts.push({
            title: $scope.theTitle,
            link: $scope.theLink,
            upvotes: 0,
            comments: [
                {author: 'Joe', body: 'Cool post!', upvotes: 0},
                {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
            ]
        });
        $scope.theTitle = '';
        $scope.theLink = '';
    }
    $scope.upvote = function(post){
        post.upvotes++;
    }
}]);

app.controller('PostsCtrl', [
'$scope',
'$stateParams',
'posts',
function($scope, $stateParams, posts){
    $scope.post = posts.posts[$stateParams.id];
    $scope.addComment = function(){
        if($scope.body === '') { return; }
        $scope.post.comments.push({
            body: $scope.body,
            author: 'user',
            upvotes: 0
        });
        $scope.body = '';
    };
}]);
<html>
<head>
    <title>Flapper 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>

<script type="text/ng-template" id="/home.html">
    <div class="page-header">
        <h1>Flapper News</h1>
    </div>
</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 ng-app="flapperNews" ng-controller="MainCtrl">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <ui-view></ui-view>

            <div ng-repeat="post in posts | orderBy:'-upvotes'">
                <span class="glyphicon glyphicon-thumbs-up"
                    ng-click="upvote(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>
                            <a href="#/posts/{{$index}}">Comments</a>
                        </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="theTitle"></input>
                </div>
                <div class="form-group">
                    <input type="text"
                        class="form-control"
                        placeholder="Link"
                        ng-model="theLink"></input>
                </div>
                <button type="submit" class="btn btn-primary">Post</button>
            </form>

        </div>
    </div>
</body>
</html>

【讨论】:

  • 抱歉,评论点赞似乎不起作用? 编辑: 哈哈,你们摇滚 :)
  • 我认为你的用户太新了。
  • 哦,我的意思是在 Angular 网站上。在 jsbin 中,只需将文件链接粘贴到 chromium (file://path_to_file) 中,我无法像单击 Run code sn-p 按钮时那样弹出 cmets 页面。抱歉给您添麻烦了,我知道您很忙,您有什么想法吗?
  • 哦,这更有意义。
  • 对不起,我没有足够的代表来聊天,但我编辑了上面的评论,你介意看一下吗?
【解决方案2】:

如果有人在复制 kiswa 的代码 sn-p 时遇到问题,我发现将脚本移动到具有 ng-app 的 body 元素下方可以解决问题(目前它们位于打开的 body 标签上方)。

所以如果你把代码修改成这样:

<!--code-->

</head>

<body ng-app="flapperNews" ng-controller="MainCtrl">

<script type="text/ng-template" id="/home.html">
    <div class="page-header">
        <h1>Flapper News</h1>
    </div>
</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>

<!--rest of code-->

它应该可以解决问题。原因是因为在body中定义了ng-app:

<body ng-app="flapperNews" ng-controller="MainCtrl">

【讨论】:

    【解决方案3】:

    让它通过 linter 会有很长的路要走。

    无论如何,

    这里少了一个逗号,多了一个分号:

    .state('home', {
        url: '/home',
        templateUrl: '/home.html'  // <-- need a comma
        controller: 'MainCtrl'
    });  // <-- remove this semicolon
    .state('posts', {
        url: '/posts/{id}',
        templateUrl: '/posts.html',
        controller: 'PostsCtrl'
    });
    

    这里的 upvotes 值后面需要一个逗号:

    $scope.posts.push({
        title: $scope.theTitle,
        link: $scope.theLink,
        upvotes: 0  // <-- need a comma
        comments: [
            {author: 'Joe', body: 'Cool post!', upvotes: 0},
            {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
        ]
    });
    

    这也假设您正在通过本地服务器运行,而不仅仅是在浏览器中加载index.html。这解决了jsbin中的问题

    【讨论】:

      猜你喜欢
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 2019-10-01
      相关资源
      最近更新 更多