Try using the $timeout service instead of setTimeout.
Also, calling $scope.$apply is mostly unnecessary.
var app = angular.module('app', []);
app.controller('ControllerA', ['$scope', '$timeout', 'Notification',
function ($scope, $timeout, Notification) {
var i = 0;
Notification.set('Notification ' + i++);
$timeout(function () {
Notification.set('Notification ' + i++)
}, 2000);
}
]);
app.controller('ControllerB', ['$scope', 'Notification',
function ($scope, Notification) {
$scope.notifications = Notification.notifications;
$scope.$watch('notifications', function () {
console.log($scope.notifications);
})
}
]);
app.factory('Notification', ['$timeout',
function ($timeout) {
var notifications = [];
return {
notifications: notifications,
set: function (name) {
notifications.push(name);
$timeout(function () {
notifications.shift();
console.log(notifications);
}, 5000)
console.log(notifications);
}
};
}
]);