我正在尝试使用 AngularJS 创建一个简单的消息传递服务(请参阅 jsFiddle),并且它大部分都在工作。但是,我的“clearAlerts()”方法似乎没有任何效果。我是 Angular 的新手,所以我不确定我做错了什么?
这是 jsFiddle http://jsfiddle.net/uberspeck/j46Yh/
...和代码
var App = angular.module('App', []);
function AlertsCtrl($scope, alertsManager) {
$scope.alerts = alertsManager.alerts;
}
function FooCtrl($scope, alertsManager) {
$scope.doGood = function() {
alertsManager.addAlert('Yay!', 'alert-success');
};
$scope.doEvil = function() {
alertsManager.addAlert('Noooo!', 'alert-error');
};
$scope.reset = function() {
alertsManager.clearAlerts();
};
}
App.factory('alertsManager', function() {
return {
alerts: {},
addAlert: function(message, type) {
this.alerts[type] = this.alerts[type] || [];
this.alerts[type].push(message);
},
clearAlerts: function() {
this.alerts = {}; // <= not working??
}
};
});