我有以下代码用于推送警报。但有趣的是,当它通过closeAlert()时,this.alerts和showAlert()中的本地警报在_difference之后不会同步值。
public showAlert(containerName: string, alertType: string, message: string, dismissTimeout?: number): void {
var alerts = this.alerts;
var alert = <ICustomAlert>{
dismissTimeout: dismissTimeout,
message: message,
type: alertType,
container: containerName
};
if (_.findWhere(alerts, <ICustomAlert>{ message: message, type: alertType, container: containerName })) {
this.closeAlert(containerName);
}
alerts.push(alert);
}
public closeAlert(containerName: string = "default", index?: number): void {
var alerts = this.alerts;
var withoutList = _.where(alerts, { container: containerName });
if (angular.isDefined(index)) {
withoutList = withoutList.splice(index, 1);
}
this.alerts = _.difference(alerts, withoutList);
}
我的初步修复是将alerts.push修改为:
this.alerts.push(alert);
我想了解为什么当我们在showAlert()中使用alerts.push时,数组没有同步。感谢。