[英]Is there a way to conditionally turn off reloadOnSearch in a controller?
I have a controller where I sometimes update the location using $location.search({param1: newParam1Value)
. 我有一个控制器,我有时使用
$location.search({param1: newParam1Value)
更新位置。
When I purposely change $location.search in the controller I would like to prevent a reload. 当我故意在控制器中更改$ location.search时,我想阻止重新加载。 Otherwise, I want to keep the default
reloadOnSearch
behavior for this route/state. 否则,我想保留此路由/状态的默认
reloadOnSearch
行为。
I can set reloadOnSearch
to false when I define the state but then it will always be false. 我在定义状态时可以将
reloadOnSearch
设置为false,但它始终为false。
Is there a way to toggle the reloadOnSearch
for the route from within the controller? 有没有办法从控制器内切换路径的
reloadOnSearch
? Alternatively, can I keep reloadOnSearch: true
and just prevent reload some other way for the one instance where I don't want it to happen? 或者,我可以保持
reloadOnSearch: true
并且只是阻止为我不希望它发生的一个实例重新加载其他方式吗?
I am using ui.router
. 我正在使用
ui.router
。
I needed this same functionality, and here's what I came up with: 我需要相同的功能,这就是我想出的:
As stated elsewhere, you need to turn reloadOnSearch = false
in the route. 如其他地方所述,您需要在路由中转换
reloadOnSearch = false
。 But MOST of the time, you want it to act like it used to with it set to true
. 但是大多数时候,你希望它像过去那样行事,设置为
true
。 You just want to skip in certain circumstances. 你只想在某些情况下跳过。 So I added this to my controller:
所以我把它添加到我的控制器:
$rootScope.skipNextSearchChangeReload = false;
$scope.$on('$routeUpdate', function(){
if (!$rootScope.skipNextSearchChangeReload) {
$route.reload();
} else {
// skip reload, but reset to stop skipping
$rootScope.skipNextSearchChangeReload = false;
}
});
var updateSearchWithoutReload = function(key, values) {
$rootScope.skipNextSearchChangeReload = true;
$location.search(key, values);
};
then all changes to the url will still reload as usual. 然后对网址的所有更改仍将像往常一样重新加载。 But when you want to skip the reload, you call
updateSearchWithoutReload(key, values);
但是当你想跳过重新加载时,你调用
updateSearchWithoutReload(key, values);
and it will skip for just that call. 它会跳过那个电话。 Then, after having skipped, it will reset to not skip reload.
然后,在跳过之后,它将重置为不跳过重新加载。
I had tried just setting $rootScope.skipNextSearchChangeReload = true
and then $location.search(k, v)
and then $rootScope.skipNextSearchChangeReload = false
, but apparently the events hadn't propagated all the way through and skipNextSearchChangeReload
was set to false
before it had skipped. 我曾尝试设置
$rootScope.skipNextSearchChangeReload = true
然后$location.search(k, v)
然后$rootScope.skipNextSearchChangeReload = false
,但显然事件没有一直传播, skipNextSearchChangeReload
设置为false
之前它跳过了。 So this was the best I could come up with. 所以这是我能想到的最好的。
Yes!! 是!! you can use
reloadOnSearch
conditionally. 你可以有条件地使用
reloadOnSearch
。 We know that state gets reloaded with $location.search() event. 我们知道使用$ location.search()事件重新加载状态。 If your use case is like this, that in some case you want to reload state and in other case you don't, then use
reloadOnSearch
like this: 如果您的用例是这样的,在某些情况下您想要重新加载状态,而在其他情况下则不需要,然后使用
reloadOnSearch
如下所示:
$scope.myClickEvent = function () {
// prevent a reload
$state.current.reloadOnSearch = false;
$location.search('page', 2);
// Do your code...
loadData();
}
/* API call to load data */ / * API调用加载数据* /
var function loadData() {
// Do your code...
// Allow to reload state
$state.current.reloadOnSearch = undefined;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.