指令模板中的 Ng-show

Ng-show in a directive template


我正在尝试使用 Angularjs 和 MongoDB 制作一个简单的应用程序。我有一些文章可以添加评论并显示它们。
评论部分位于指令上定义的模板中,指令本身与另一个控制器位于主页模板中。
要清楚(或可能不是?):HomeTemplate [with MainCtrl] 拥有一个 COMMENT 元素 [with his directive]。

这是我的指令

app.directive('Comment', function(){
return{
    templateUrl: '/comments.html',
    scope:{
        post:'=datapost'
    },
    controller: ['$scope', 'posts', 'auth', function($scope, posts,auth){
        $scope.isLoggedIn = auth.isLoggedIn;


        $scope.addComment = function(){             
            if($scope.commentToAdd === '')
                return;

            $scope.isLoading = true;
            posts.addComment($scope.post._id, {
                body: $scope.commentToAdd,
                author: auth.currentUser()
            }).success(function(comment){
                $scope.post.comments.push(comment);
            });
            $scope.commentToAdd = '';
            $scope.isLoading = false;
        };  
    }]
  }
});

Auth 和 Posts 指令稍后定义

这是我的 MainCtrl,我应该在其中触发事件:

$scope.showComments = function(){
        $scope.showingComments = true;
    };

这是我的 HTML :
<div ng-show="showingComments =!showingComments"> <div comment datapost="post" ></div> </div>

我不太确定你的问题是什么但是:

<div ng-show="showingComments =!showingComments">

应该是

<div ng-show="showingComments">

=! 不是有效的运算符,您正在将同一个变量与其自身进行比较

使用 ng-show="showingComments" 显示 true,或 ng-show="!showingComments" 显示 false