TypeError:无法使用Angular读取undefined的属性'comments'

Pet*_*sma 6 angularjs

我正在关注本教程当我尝试在帖子上发表评论时,我TypeError: Cannot read property 'comments' of undefined在控制台中收到错误消息.

mainCtrl,

.controller('mainCtrl', ['$scope', 'posts',
  function($scope, posts){
    $scope.posts = posts.posts;

  $scope.addPost = function(){
    if(!$scope.title || $scope.title === '') { alert("Field can't left blank"); return; }

    $scope.posts.push({
      title: $scope.title,
      upvotes: 0,
      comments: [
        {author: 'Joe', body: 'Cool post!', upvotes: 0},
        {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
      ]
    });
  };
  }
])
Run Code Online (Sandbox Code Playgroud)

和postCtrl,

.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 = '';
    };

  }
])
Run Code Online (Sandbox Code Playgroud)

两个控制器都在mainCtrl.js.

这是我的home.html和post.html部分,它们通过router-ui包含在内.

%script{:id => "/home.html", :type => "text/ng-template"}
  %h1
    Flappernews

  %a{:href => "#/posts/{{$index}}"} Comments

%script{:id => "/posts.html", :type => "text/ng-template"}
  %h2
    Below here should be comments
  %span
    {{ post.comments }}
  %div{"ng-repeat" => "comment in post.comments | orderBy:'-upvotes'"}
    {{comment.upvotes}} - by {{comment.author}}
    %span{:style => "font-size:20px; margin-left:10px;"}
      {{comment.body}}

  %form{"ng-submit" => "addComment()"}
    %h3 Add a new comment
    .form-group
      %input.form-control{"ng-model" => "body", :placeholder => "Comment", :type => "text"}
    %button.btn.btn-primary{:type => "submit"} Post
Run Code Online (Sandbox Code Playgroud)

当我访问主页时,我被重定向到localhost:3000/#/home我可以输入标题并发布.当我点击comments链接时,我被重定向到http://localhost:3000/#/posts/,当我尝试发表评论时,我得到了

TypeError: Cannot read property 'comments' of undefined at Scope.$scope.addComment 错误.

Zai*_*din 1

您已经定义了两个单独的控制器,不需要这样做。如果您再次查看该教程,您会发现 addPost 和 addComment 函数位于单个控制器内,如下所示

   .controller('PostsCtrl', [
    '$scope',
    '$stateParams',
    'posts',
    function($scope, $stateParams, posts){

    // Fetch post object from state parameter.
    $scope.post = posts.posts[$stateParams.id];

    // Add Post.
    $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}
      ]
    });

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

    }]);
Run Code Online (Sandbox Code Playgroud)

添加帖子和评论适用于预定义 comments 属性的单个 Post 对象。