【问题标题】:Atmosphere and Angular JS how toAtmosphere 和 Angular JS 如何
【发布时间】:2015-05-06 06:50:24
【问题描述】:

我是一个氛围和 Angular 新手,我真的很难找到答案!也许我问错了问题。

我正在使用 Atmosphere 设置通知。如果我将 API URL 直接发布到浏览器中,我可以打开 websocket 并观察更新。

在 Angular 中,我有一个 ng-repeat 循环,我希望在每次新更新都会向 websocket 添加一个新对象时运行它。

<li ng-repeat="notification in notifications track by $index">

我正在使用 angular watch 来检查更新,但它不会拾取添加到数组中的新对象。这是我的代码:

// notification alerts
$scope.notifications = [];

notificationsService.notificationAlerts().then(function success(response) {                
    var jsonStringArray = response.data.split('|');
    $scope.notifications = $.map(jsonStringArray, function(n, i){
        if (n !== ""){
            return JSON.parse(n);
        }
    });
    console.log('Connect', response);               
});

$scope.$watch('notifications', function(newVal, oldVal){
    console.log('Watch', $scope.notifications);
}, true);

希望我已经说清楚了,如果我需要详细说明,或者我问错了问题,请告诉我。谢谢!

【问题讨论】:

    标签: angularjs websocket atmosphere


    【解决方案1】:

    好的,我设法解决了这个问题,以后遇到的任何人都会遇到这种情况。这是最终的 JS:

    // add number of notifications to ".notifications-number"
    function updateNumberOfNotifications(){
        var numberOfNotifications = $("ul.notifications-list li").not(".nocount").length;
        if (numberOfNotifications < 1) {
            $(".notifications-number, .notifications-list").addClass("hidden");
        } else {
            $(".notifications-number").html(numberOfNotifications);
            $(".notifications-number, .notifications-list").removeClass("hidden");
        }                
    }            
    
    // notification alert variables
    $scope.notifications = [];
    var socket = atmosphere;
    var subSocket;
    
    // subscribe
    function subscribe() {
        var request = {
            url : "/service/notifier",
            transport: 'long-polling'
        };
    
        request.onMessage = function (response) {
            //console.log('response', response);
            var jsonStringArray = response.responseBody.split('|');
            // console.log('json string array', jsonStringArray);
            $.each(jsonStringArray, function(index, elem){
                if (elem != ""){
                    $scope.notifications.push(JSON.parse(elem));
                    console.log("object", JSON.parse(elem));
                }                        
            });
            //$scope.notifications.push($scope.newNotification);
            $scope.$apply();                  
    
            updateNumberOfNotifications();          
    
            // console.log('$scope.notifications', $scope.notifications);             
        };
    
        subSocket = socket.subscribe(request);
    }
    
    function unsubscribe(){
        socket.unsubscribe();
    }
    
    // subscribe on load and update notifications
    updateNumberOfNotifications();
    subscribe();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2013-09-18
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多