将对象推送到$ scope数组后,ng-repeat不会更新

时间:2017-12-15 01:58:29

标签: angularjs

var chat = $.connection.tDKTHub;
        chat.client.NewNotification = function (notification) {
            _notification = JSON.parse(notification);                
            $scope.Notifications.push(_notification);
            console.log($scope.Notifications);
        }

<div class="notification-container container">
            <br />
            <div class="notification row"
                 ng-class="{unread : notification.STATUS==0}"
                 ng-repeat="notification in Notifications">
                <div class="col-2">
                    <img style="width:50px;height:50px" />
                </div>
                <div class="col-10">
                    <p>{{notification.SENDER_FULLNAME}} {{notification.TYPE_ACTION}} {{notification.MESSAGE}}</p>
                </div>
            </div>

        </div>

即使对象被推入$ scope数组,UI也不会更新。谁知道这里发生了什么? Link to image showing the object was added to the array

1 个答案:

答案 0 :(得分:2)

当angular未检测到范围变量中的更改时,您可以通过调用 $scope.$apply();

手动绑定更改
$scope.Notifications.push(_notification);
$scope.$apply();