1

我的观点是这样写的:

<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 评估为真。我进行了测试,它确实评估为真,但它仍然不显示按钮。知道为什么吗?

4

2 回答 2

3

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

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

现在,您可以看到您的函数没有返回任何内容(这意味着它返回undefined,这是错误的)。

代码包含的 return 语句从传递给 then() 函数的回调返回。该语句是异步执行的, isPostedUser()返回之后。

于 2015-04-25T19:41:31.463 回答
0

所以我将其修复如下:

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

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

于 2015-04-25T19:59:51.453 回答