【发布时间】:2015-09-15 15:22:03
【问题描述】:
我有一个 AngularJs 应用程序正在检测状态的变化(使用 ui.router),以向用户提供保存未保存更改的选项。现在我正在使用确认对话框来执行此操作:
$scope.$on('$stateChangeStart', () => {
if (self.changed && confirm('There are unsaved changes. Do you want to save them?'))
this.save();
});
我想将其更改为使用 bootstrap ui 库中的 $modal 对话框。我遇到的问题是,当$modal.open() 调用即时返回(异步)时,状态会在打开对话框之前发生变化,并且从未打开过。
$scope.$on('$stateChangeStart', () => {
if (self.changed)
this.$dialog.open({...}).result.then(()=>{
this.save();
});
});
有没有办法克服这个问题,还是我坚持使用普通的 javascript 确认对话框?
【问题讨论】:
-
是什么触发了状态变化?可能需要抓住它,甚至不进入改变
-
可能是 ui-sref 链接或通过
$state.go手动更改状态。无论出于何种原因,我都想检测用户何时退出该状态。 -
我唯一能想到的就是根据可能存储在 $rootScope 或服务中的标志来阻止事件,并使用模式事件切换标志。如果全部通过
$state.go()会简单得多
标签: angularjs twitter-bootstrap dialog typescript angular-ui-router