Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS-ngShow不工作_Angularjs_Angularjs Ng Show - Fatal编程技术网

AngularJS-ngShow不工作

AngularJS-ngShow不工作,angularjs,angularjs-ng-show,Angularjs,Angularjs Ng Show,我的看法如下: <ul class="commentslist cf"> <li class="cf" ng-repeat="(key,comment) in activity.comments"> <div class="comment">{{comment.name}} <div class="buttons" ng-show="isPostedUser(activity.$id, key,

我的看法如下:

<ul class="commentslist cf">
        <li class="cf" ng-repeat="(key,comment) in activity.comments">
          <div class="comment">{{comment.name}}
            <div class="buttons" ng-show="isPostedUser(activity.$id, key, currentUser)">
              <button class="btn btn-delete tooltip"
                confirmation-needed = "Are you sure you want to delete this activity?"
                ng-click="deleteComment(activity.$id,key)">
                <span>Delete this comment</span></button>
            </div><!-- buttons -->
          </div><!-- comment -->
        </li>            
</ul>
$scope.isPostedUser = function(actId, key, user) {
    var refComment = new Firebase(FIREBASE_URL + "users/" + $scope.whichuser + "/activities/" + actId + "/comments/" + key);
    var commentObj = $firebase(refComment).$asObject();
    commentObj.$bindTo($scope, "data");
    return $scope.data.giver === user.$id;
};
})


此函数的目的是仅在isPostedUser计算为true时显示删除按钮。我进行了测试,它的评估结果为真,但它仍然没有显示按钮。知道为什么吗?

让我们用适当的缩进重写函数:

$scope.isPostedUser = function(actId, key, user) {
    var refComment = new Firebase(FIREBASE_URL + "users/" + $scope.whichuser + "/activities/" + actId + "/comments/" + key);
    var commentObj = $firebase(refComment).$asObject();
    commentObj.$bindTo($scope, "data").then(function() {
        return $scope.data.giver === user.$id;
    });
};
现在,您可以看到您的函数没有返回任何内容(这意味着它返回
未定义的
,这是falsy)


代码包含的return语句从传递给then()函数的回调返回。此语句在返回后以异步方式执行。
isPostedUser()

因此我将其修复如下:

<ul class="commentslist cf">
        <li class="cf" ng-repeat="(key,comment) in activity.comments">
          <div class="comment">{{comment.name}}
            <div class="buttons" ng-show="isPostedUser(activity.$id, key, currentUser)">
              <button class="btn btn-delete tooltip"
                confirmation-needed = "Are you sure you want to delete this activity?"
                ng-click="deleteComment(activity.$id,key)">
                <span>Delete this comment</span></button>
            </div><!-- buttons -->
          </div><!-- comment -->
        </li>            
</ul>
$scope.isPostedUser = function(actId, key, user) {
    var refComment = new Firebase(FIREBASE_URL + "users/" + $scope.whichuser + "/activities/" + actId + "/comments/" + key);
    var commentObj = $firebase(refComment).$asObject();
    commentObj.$bindTo($scope, "data");
    return $scope.data.giver === user.$id;
};

如果有更好的解决方案,请告诉我。

非常感谢。顺便说一句,很抱歉没有缩进代码!我的观点是,缺乏压痕首先会伤害你。通过适当的缩进,代码的结构变得明显,您可以更轻松地阅读和理解自己的代码。