告诉 ui-路由器不要改变状态定义的位置
tell ui-router to not change location on state defintion
在ui-router中,当你调用state.go时,你可以通过传递{location:false}
来定义防止位置改变的选项。但是,如何使用 $stateProvider.state
定义一个状态,这样它就不会改变位置?我已经为自定义 URL 处理定义了一个规则,但现在它正在更改位置值。
我有 foo
状态 -
$stateProvider
.state('foo', {
url: '/foo'
templateUrl: '',
controller: function($scope) {
console.log('made it!');
}
});
我有一个自定义规则 -
$urlRouterProvider.rule(function ($injector, $location) {
// some logic
return '/foo';
});
除了更改位置并将 #/foo
附加到该位置外,这完全符合我的要求。我不想附加它。
我创造了working example here. It is extended version of the solution presented here: How not to change url when show 404 error page with ui-router
有一段代码,应该显示如何使用 "foo" 状态而不用 url 改变。
首先,有我们的规则,它将重定向到 'foo'
状态——以防万一 /home
作为 url 传递(一个例子决定)
$urlRouterProvider.rule(function($injector, $location) {
if ($location.path() == !"/home") {
return false;
}
var $state = $injector.get("$state");
// move to state, but do not change url
$state.go("foo", {
location: false
})
return true;
});
这是我们的示例状态,其中确实有 url 设置
// States
$stateProvider
.state('home', {
url: "/home",
templateUrl: 'tpl.html',
})
.state('parent', {
url: "/parent",
templateUrl: 'tpl.html',
})
.state('parent.child', {
url: "/child",
templateUrl: 'tpl.html',
controller: 'ChildCtrl',
})
Foo 没有 url
.state('foo', {
//url: '/foo',
templateUrl: 'tpl.html',
});
检查一下here
在ui-router中,当你调用state.go时,你可以通过传递{location:false}
来定义防止位置改变的选项。但是,如何使用 $stateProvider.state
定义一个状态,这样它就不会改变位置?我已经为自定义 URL 处理定义了一个规则,但现在它正在更改位置值。
我有 foo
状态 -
$stateProvider
.state('foo', {
url: '/foo'
templateUrl: '',
controller: function($scope) {
console.log('made it!');
}
});
我有一个自定义规则 -
$urlRouterProvider.rule(function ($injector, $location) {
// some logic
return '/foo';
});
除了更改位置并将 #/foo
附加到该位置外,这完全符合我的要求。我不想附加它。
我创造了working example here. It is extended version of the solution presented here: How not to change url when show 404 error page with ui-router
有一段代码,应该显示如何使用 "foo" 状态而不用 url 改变。
首先,有我们的规则,它将重定向到 'foo'
状态——以防万一 /home
作为 url 传递(一个例子决定)
$urlRouterProvider.rule(function($injector, $location) {
if ($location.path() == !"/home") {
return false;
}
var $state = $injector.get("$state");
// move to state, but do not change url
$state.go("foo", {
location: false
})
return true;
});
这是我们的示例状态,其中确实有 url 设置
// States
$stateProvider
.state('home', {
url: "/home",
templateUrl: 'tpl.html',
})
.state('parent', {
url: "/parent",
templateUrl: 'tpl.html',
})
.state('parent.child', {
url: "/child",
templateUrl: 'tpl.html',
controller: 'ChildCtrl',
})
Foo 没有 url
.state('foo', {
//url: '/foo',
templateUrl: 'tpl.html',
});
检查一下here