【问题标题】:AngularJS - ngShow is not workingAngularJS - ngShow 不工作
【发布时间】:2015-04-25 19:31:43
【问题描述】:

我的观点是这样写的:

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

【问题讨论】:

    标签: angularjs angularjs-ng-show


    【解决方案1】:

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

    $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() 返回之后。

    【讨论】:

    • 非常感谢。顺便说一句,很抱歉没有缩进代码!
    • 我的意思是缺少缩进首先会伤害你。通过适当的缩进,代码的结构变得清晰,您可以更轻松地阅读和理解自己的代码。
    【解决方案2】:

    所以我将其修复如下:

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多