AngularJs:如何检测用户何时在浏览器的地址栏中输入无效状态?
AngualrJs: how to detect when user enters an invalid state in the brower's address bar?
有些相关:Angular - ui-router get previous state。
我有搜索结果视图。显然,用户将此 URL 输入浏览器的地址栏并导航到那里是没有意义的。
我想检测他是否这样做并发出警告。
但是,怎么办?如果用户直接去那里,则没有状态转换。那么,我该如何检测呢?
当用户手动输入 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();
})
})
我选择了一项服务,我已经使用该服务在更改状态时在控制器之间共享数据。原因是过渡状态更改 OnSuccess() 第一次不起作用,根据
https://github.com/angular-ui/ui-router/issues/3536 可能还有其他人
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=
Self.setState = function (currentState, newState) {
Self.setPreviousState(currentState);
console.log("Change state from [" + currentState + "] to [" + newState + "]");
Self.state = newState;
$state.go(newState);
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=
Self.getState = function () {
console.log("Current state is [" + Self.state) + "]";
return Self.state;
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Self.getPreviousState = function () {
return Self.previousState;
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Self.setPreviousState = function (previousState) {
Self.previousState = previousState;
}
有些相关:Angular - ui-router get previous state。
我有搜索结果视图。显然,用户将此 URL 输入浏览器的地址栏并导航到那里是没有意义的。
我想检测他是否这样做并发出警告。
但是,怎么办?如果用户直接去那里,则没有状态转换。那么,我该如何检测呢?
当用户手动输入 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();
})
})
我选择了一项服务,我已经使用该服务在更改状态时在控制器之间共享数据。原因是过渡状态更改 OnSuccess() 第一次不起作用,根据
https://github.com/angular-ui/ui-router/issues/3536 可能还有其他人
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=
Self.setState = function (currentState, newState) {
Self.setPreviousState(currentState);
console.log("Change state from [" + currentState + "] to [" + newState + "]");
Self.state = newState;
$state.go(newState);
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=
Self.getState = function () {
console.log("Current state is [" + Self.state) + "]";
return Self.state;
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Self.getPreviousState = function () {
return Self.previousState;
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Self.setPreviousState = function (previousState) {
Self.previousState = previousState;
}