简体   繁体   English

Ng级无法检测到变化

[英]Ng-Class doesn't detect changes

On page I have ng-repeat which iterates through items in collection. 在页面上,我有ng-repeat ,它遍历集合中的项目。 Every item has property status according to which I add some class or don't. 每个项目都具有属性status根据该status我可以添加或不添加类。 And every item has button, onclick it change item status and make PUT request to API. 每个项目都有一个按钮,单击它可以更改项目状态并向API发出PUT请求。

As my collection on page is updated every 2 minutes, I have problem after few updates. 由于页面上的收藏集每2分钟更新一次,因此几次更新后我遇到了问题。 After 5-6 minutes I make click on button, which make PUT request (successful), but my ng-class function doens't detect that status of item is changed. 5-6分钟后,我单击按钮,这使PUT请求成功(但成功),但是我的ng-class函数没有检测到项目status已更改。

<div class="one-third" ng-repeat="request in requests track by request.id">
        <div class="incoming_request" ng-class="actionClass(request)">
            <h2 ng-class="DayTypesClasses[request.typeId]" ng-bind="request.type"></h2>
            <hr>
            <div class="request_description">
                <p><span>Description:</span></p>
                <p ng-bind="request.baseComment"></p>
            </div>
            <div class="request_resolve">
                <hr>
                <div class="textarea-holder">
                    <textarea placeholder="Your comment..." ng-model="request.newComment" ng-model-options="{ updateOn: 'default blur'}"></textarea>
                </div>
                <div class="button-container">
                    <button ng-click="approve(request);" class="btn btn-primary">Confirm</button>
                    <button ng-click="reject(request);" class="btn btn-default pull-right" am-hide-request-resolve-div>Reject</button>
                </div>
            </div>
        </div>
</div>

and JS-code 和JS代码

$scope.approve = function (request) {
        var status = State.APPROVED;

        var currentUserComment = request.comments.filter(function(comm) {
                return comm.userId == user.id && comm.status == "Pending";
            })[0];
        currentUserComment.status = status; //change status
        currentUserComment.text = request.newComment;

        delete request.newComment;

        if (!currentUserComment) {
            request.isProcessing = false;
            return;
        }

        Comments.update(currentUserComment, function() {
            // $rootScope.$broadcast('daysUpdated');   
        });

        request.isProcessing = false;
    };

Ng-class function : Ng-class功能:

$scope.actionClass = function(request) {
        var currentUserComment = request.comments.filter(function(comment) {
            return comment.userId == user.id;
        })[0];
        //after click here status for every item isn't changed. Tested in debug mode
        if (!currentUserComment || currentUserComment.status == State.APPROVED || currentUserComment.status == State.REJECTED) {
            return 'you-approved';
        }
    }

So as result after few updates of my collection ( requests collection I mean) onclick js isn't able to add class you-approved . 因此,在对我的集合(我的意思是requests集合)进行几次更新后,onclick js无法添加you-approved类。


I've missed one imporatnt detail - I change status not to item request but for item, that exists in request.comments . 我错过了一个重要的细节-我将状态更改为request.comments中存在的项目,而不是项目request So I can't just write ng-class="{'you-approved': request.approved}" 因此,我不能只写ng-class="{'you-approved': request.approved}"


As @Cyril suggested I've tried to use this peace of code in my controller 正如@Cyril所建议的那样,我尝试在控制器中使用这种代码的稳定性

$scope.$apply(function(){$scope.requests = CurrentUserData.getRequests();}) but receive error $digest already in progress . $scope.$apply(function(){$scope.requests = CurrentUserData.getRequests();})但收到错误$digest already in progress

Also I've tried to use angualr service $interval instead of window.setInterval function that updates collection requests every 2 minutes, but it doesn't seem to help. 另外,我尝试使用angualr服务$interval而不是window.setInterval函数,该函数每2分钟更新一次收集requests ,但这似乎没有帮助。

How about: 怎么样:

ng-class="{'you-approved': !currentUserComment || currentUserComment.status == State.APPROVED || currentUserComment.status == State.REJECTED}"

Here is an example of that logic using a plunker: 这是使用插销的逻辑示例:

https://plnkr.co/edit/hoTnrLBAibMJ0wd2WMzm?p=info https://plnkr.co/edit/hoTnrLBAibMJ0wd2WMzm?p=info

HTML: HTML:

<button ng-click="setApproved()">Set Approved</button>
<button ng-click="setRejected()">Set Rejected</button>
<button ng-click="setSomethingElse()">Set Something Else</button>

<hr />

<span ng-class="{'you-approved': !currentUserComment || currentUserComment.status == 'approved' || currentUserComment.status == 'rejected'}">Test</span>

Controller JS: 控制器JS:

 $scope.setApproved = function(){
      $scope.currentUserComment = {
        status: 'approved'
      }
  }

  $scope.setRejected = function(){
      $scope.currentUserComment = {
        status: 'rejected'
      }
  }

    $scope.setSomethingElse = function(){
      $scope.currentUserComment = {
        status: 'somethingElse'
      }
  }

https://docs.angularjs.org/api/ng/directive/ngClass https://docs.angularjs.org/api/ng/directive/ngClass

have a look at: 看一下:

https://docs.angularjs.org/api/ng/directive/ngClass https://docs.angularjs.org/api/ng/directive/ngClass

your ng-class expression doesnt need to return the class property but a json that holds the class name with a condition 您的ng-class表达式不需要返回class属性,而是一个带有条件的包含类名称的json

so change 所以改变

<h2 ng-class="DayTypesClasses[request.typeId]" ng-bind="request.type"></h2>

to: 至:

<h2 ng-class="{'you-approved': request.approved}" ng-bind="request.type"></h2>

and JS-code update the approved parameter 和JS代码更新批准的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM