AngularJS - ngShow 不工作

AngularJS - ngShow is not working

我的观点是这样写的:

<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").then(function() {
  return $scope.data.giver === user.$id;
});

};

此函数的目的是仅在 isPostedUser 计算结果为 true 时显示删除按钮。我测试过,它确实评估为 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;
    });
};

现在,您可以看到您的函数没有 return 任何东西(这意味着它 returns undefined,这是虚假的)。

return 语句,代码包含来自传递给 then() 函数的回调的 returns。该语句异步执行,after isPostedUser() has returned.

所以我修改如下:

$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;
};

如果谁有更好的解决办法,请告诉我。