3
このコードを使用して、コントローラ、ディレクティブおよびサービス間でメッセージをパブリッシュおよびサブスクライブするファクトリを作成しています。AngularJSを使用してSubscribeサービスを公開
angular.module('app', []);
angular.module('app').controller('TheCtrl', function($scope, NotifyingService) {
$scope.notifications = 0;
$scope.notify = function() {
NotifyingService.publish();
};
// ... stuff ...
NotifyingService.subscribe($scope, function somethingChanged() {
// Handle notification
$scope.notifications++;
});
});
angular.module('app').factory('NotifyingService', function($rootScope) {
return {
subscribe: function(scope, callback) {
var handler = $rootScope.$on('notifying-service-event', callback);
scope.$on('$destroy', handler);
},
publish: function() {
$rootScope.$emit('notifying-service-event');
}
};
});
それは正常に動作しているが、私は私はそれをどのように行うか、というの加入玉葉誰かに公開していながらデータを渡したいです。 4の値を公開するとします。これをどのように実行しますか?