AngularJS - ngShow无效

时间:2015-04-25 19:31:44

标签: 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, 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,但它仍然没有显示按钮。知道为什么吗?

2 个答案:

答案 0 :(得分: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()返回后

答案 1 :(得分: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;
};

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