我正在嘗試爲Pusher(http://pusher.com)編寫AngularJS庫,並且遇到了一些問題,這些問題與我對摘要循環的理解以及它的工作方式有關。我正在寫Pusher JavaScript庫的基本上是一個Angular包裝器。如何將AngularJS服務與摘要循環集成
我面臨的問題是,當Pusher事件觸發並且我的應用程序訂閱了它時,它會收到消息但不更新訂閱的設置範圍。
我此刻的下面的代碼:
angular.module('pusher-angular', [])
.provider('PusherService', function() {
var apiKey = '';
var initOptions = {};
this.setOptions = function (options) {
initOptions = options || initOptions;
return this;
};
this.setToken = function (token) {
apiKey = token || apiKey;
return this;
};
this.$get = ['$window',
function ($window) {
var pusher = new $window.Pusher(apiKey, initOptions);
return pusher;
}];
})
.factory('Pusher', ['$rootScope', '$q', 'PusherService', 'PusherEventsService',
function ($rootScope, $q, PusherService, PusherEventsService) {
var client = PusherService;
return {
subscribe: function (channelName) {
return client.subscribe(channelName);
}
}
}
]);
.controller('ItemListController', ['$scope', 'Pusher', function($scope, Pusher) {
$scope.items = [];
var channel = Pusher.subscribe('items')
channel.bind('new', function(item) {
console.log(item);
$scope.items.push(item);
})
}]);
,並在另一個文件中設置應用起來:
angular.module('myApp', [
'pusher-angular'
]).
config(['PusherServiceProvider',
function(PusherServiceProvider) {
PusherServiceProvider
.setToken('API KEY')
.setOptions({});
}
]);
我已經刪除了一些代碼,使其更簡潔。
在ItemListController
中,當從Pusher收到消息時,$scope.items
變量不會更新。
我的問題是,我怎麼能這樣做,當從Pusher收到一條消息,然後觸發一個摘要,以便範圍更新和更改反映在DOM?
編輯:我知道我可以將訂閱回調包裝在$scope.$apply()
中,但我不希望每個回調都這樣做。有沒有一種方法可以將其與服務集成?
有沒有辦法在服務內部完成它,而不是在需要更新實際範圍的Controller中? – hamchapman 2014-10-01 16:24:33
@hamchapman您可以在服務中注入'$ rootScope'並調用'$ rootScope。$ apply()'或'$ rootScope。$ digest()'。您幾乎總是應該調用'$ apply()'來確保所有內容都正確更新。 – m59 2014-10-01 16:31:30
@ m59應該在哪裏調用?當我正在嘗試時,我收到一個錯誤,表示已經有一個應用或摘要操作正在進行(https://docs.angularjs.org/error/$rootScope/inprog?p0=$digest) – hamchapman 2014-10-01 16:37:05