有点相关:Angular - ui-router get previous state。
我有搜索结果视图。显然,用户在浏览器的地址栏中输入该URL并在其中导航是没有意义的。
我想检测他是否这样做并发出警告。
但是,如何?如果用户直接去那里,则没有状态转换。那么,如何检测到它?
[当用户手动输入网址时-您的应用会重新初始化,因此请使用app.run来跟踪位置:
app.module('myModule').run(['$location'], ($location) => {
if ($location.path().startsWith('/search')) {
alert('bad boy')
}
})
或将第一个状态更改为跟踪状态名称:
app.module('myModule').run(['$transitions'], ($transitions) => {
const deregister = $transitions.onSuccess({}, (transition) => {
if (transition.to().name === 'search') {
alert('bad girl')
}
deregister();
})
})