[英]Value not updating automatically in angularJS with socket.io
以下是我的帶有socket.io
angularJS
代碼,雖然我希望它自動更新值,但僅在從視圖切換時才獲取值。 讓我知道我在做什么錯-
angular.module("app").controller("DashboardController", function($scope, SessionService) {
$scope.user = SessionService.currentUser;
// Code for realtime notifications
if (SessionService.currentUser._id) {
var socket = io('http://localhost:6868');
socket.emit('get notifications', {user_id: SessionService.currentUser._id});
socket.on('notification data', function(data){
$scope.$apply(function() {
$scope.notificationCount = data.length;
});
});
}
});
我認為您不需要在這里執行$ scope。$ apply。 您可以不使用該值分配新值。 套接字在這里作為angularjs工廠看起來更好。
查看此代碼,以了解如何將套接字用作工廠。
https://github.com/sojharo/Simplest-Mean/blob/master/public/javascripts/MyAngularApp.js
讓事情分開
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.