Angularjs LocationChangeStart事件预防默认不工作
我尝试使用以下代码阻止导航到某个urlAngularjs LocationChangeStart事件预防默认不工作,angularjs,Angularjs,我尝试使用以下代码阻止导航到某个url run(['$rootScope', '$location', function ($rootScope,$location) { $rootScope.$on('$locationChangeStart', function (event, next, current) { if (next) { if (isProhibetedUrl(next)) {
run(['$rootScope', '$location', function ($rootScope,$location) {
$rootScope.$on('$locationChangeStart', function (event, next, current) {
if (next) {
if (isProhibetedUrl(next)) {
event.preventDefault();
}
}
});
但它不起作用,我进入了10个摘要的循环。我还尝试了$routeChangeStart,但也没有成功。以下是我如何将它与$routeStartChange一起使用。这是在您定义了模块之后
app.run(['$rootScope', '$location',function ($rootScope, $location) {
$rootScope.$on('$routeChangeStart', function (event) {
if (!permitted) { // your check
event.preventDefault();
}
});
还要确保在html中包含“angular route.js”,并在模块定义中注入依赖项
var app = angular.module('myapp', ['ngRoute',.....
下面是我如何将它与$routeStartChange一起使用。这是在您定义了模块之后
app.run(['$rootScope', '$location',function ($rootScope, $location) {
$rootScope.$on('$routeChangeStart', function (event) {
if (!permitted) { // your check
event.preventDefault();
}
});
还要确保在html中包含“angular route.js”,并在模块定义中注入依赖项
var app = angular.module('myapp', ['ngRoute',.....
它工作得非常好。请参阅以下文件。尝试将相同的应用于代码 Plunkr: 所以,有一个问题,是否输入if条件如果条件满足,您可以执行console.log并检查条件。如果没有,那么试着发布一个JSFIDLE或Plunkr,其中只包含要解决的问题,这将很容易调试。谢谢 代码:
app.run(function($rootScope, $location) {
//when the route is changed scroll to the proper element.
$rootScope.$on("$locationChangeStart", function(event, next, current) {
console.log(event);
console.log(next);
console.log(current);
if(next.indexOf("two") !== -1){
console.log("preventing default");
event.preventDefault();
}
});
});
说明:
app.run(function($rootScope, $location) {
//when the route is changed scroll to the proper element.
$rootScope.$on("$locationChangeStart", function(event, next, current) {
console.log(event);
console.log(next);
console.log(current);
if(next.indexOf("two") !== -1){
console.log("preventing default");
event.preventDefault();
}
});
});
检查下一个路由的关键字
two
,如果该关键字存在,则停止传播事件。如果输入了if条件,请检查控制台。它工作正常。请参阅以下文件。尝试将相同的应用于代码
Plunkr:
所以,有一个问题,是否输入if条件如果条件满足,您可以执行console.log并检查条件。如果没有,那么试着发布一个JSFIDLE或Plunkr,其中只包含要解决的问题,这将很容易调试。谢谢
代码:
app.run(function($rootScope, $location) {
//when the route is changed scroll to the proper element.
$rootScope.$on("$locationChangeStart", function(event, next, current) {
console.log(event);
console.log(next);
console.log(current);
if(next.indexOf("two") !== -1){
console.log("preventing default");
event.preventDefault();
}
});
});
说明:
app.run(function($rootScope, $location) {
//when the route is changed scroll to the proper element.
$rootScope.$on("$locationChangeStart", function(event, next, current) {
console.log(event);
console.log(next);
console.log(current);
if(next.indexOf("two") !== -1){
console.log("preventing default");
event.preventDefault();
}
});
});
检查下一个路由的关键字
two
,如果该关键字存在,则停止传播事件。如果输入了if条件,请检查控制台。谢谢。我尝试过这个实现,但没有成功。它仍会导航到下一页,希望您有$routeProvider。当(…阻止所有设置,谢谢。我尝试了此实现,但没有成功。它仍会导航到下一页,希望您有$routeProvider。当(…阻止所有设置,