比较angularjs中的对象

时间:2014-12-30 17:06:50

标签: angularjs

我想要实现的是,如果帖子包含评论,它应该显示评论。我尝试了angular.equals,它要么不起作用,要么我不熟悉如何使用它?

这是Post和Comment的控制器。

var PostController = function ($scope, $location, Post, Comment) {
    $scope.search = function () {
        Post.query({
            offset: $scope.offset,
            limit: $scope.limit
        },
        function (data) {
            $scope.more = data.length === 20;
            $scope.posts = $scope.posts.concat(data);
        });
    };

    $scope.searchComments = function () {
        Comment.query({
        },
        function (data) {
            $scope.comments = data;
        });
    };

    $scope.showComment = function () {
        angular.equals(Post, Comment);
    }
}

以下是观点:

<ul class="posts">
    <li ng-repeat="post in posts">
        <div class="post-info">
            <h1>{{post.title}}</h1>
                <div class="right-section">
                    <div class="edit-post-button right"><a href="#/edit">Edit</a></div>
                    <span class="reputation right">{{post.reputation}}</span>
                </div>

        </div>

        <br />



        <div class="post-box">
            <p>
                {{post.content}}
            </p>
        </div> 
        <br />

        <ul>
            <li ng-repeat="comment in comments" ng-show="showComment()">
                {{comment.content}}
            </li>
        </ul>
    </li>

</ul>

也许这可以在后端完成?我不确定?

1 个答案:

答案 0 :(得分:0)

您似乎希望过滤对特定post的评论。正如您在评论中所说,Comment资源有一个pid,可以将其与特定帖子联系起来。

完成过滤的最佳方法是使用angular filter

通过这种方式,您不需要将任何内容与equals进行比较,您只需过滤标记中的注释:

    <ul>
        <li ng-repeat="comment in comments | filter:{ pid: post.id }">
            {{comment.content}}
        </li>
    </ul>