如果AngularJS与值匹配,则在加载时更改单个URL参数 [英] AngularJS change single URL parameter on load if it matches a value

查看:137
本文介绍了如果AngularJS与值匹配,则在加载时更改单个URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Web应用程序,当您浏览网站时,参数会传递给视图.这些参数由用户过滤器设置.

I'm working on a web app where parameters get passed to views as you navigate through the site. These parameters are set by user filters.

我遇到了一个问题,即某些视图不支持其中一个过滤器的特定值,并且在这种情况下,需要将参数重置为两种可能性中的一种.

I've hit a problem whereby some views don't support a specific value from one of the filters, and where this happens the parameter needs to be reset to one out of two possibilities.

我一直在使用$location.search({ type: "blue" }),但是我已经意识到,如果url/视图中还有其他参数,这些参数将被替换为仅使用该代码设置的参数.

I've been using $location.search({ type: "blue" }) but I've realised that if there are other parameters in the url/on the view, these get removed in place for just the one that is set using that code.

基本上,在某些视图上,如果type参数是all,则需要将其更改为blue,而在其他视图上,则需要将其更改为red.

Basically, on some views if the type parameter is all it needs to change to blue, and on others it would need to change to red.

不幸的是,此参数是由当前应用的过滤器在链接中设置的,因此它不像更改href那样简单.

Unfortunately this parameter is set in the link by the current filter that is applied, so its not as simple as just changing the href.

在此示例中您可以在这里看到,我试图将仅使用数组设置了blue的视图(以及在链接中)从只能设置为red的视图更改为red的路径.

You can see here in this example I'm trying to change views that can only be set as red to red when coming from a view where blue was set (and in the link) using an array of paths.

for (let i = 0; i < redOnlyViews.length; i++) {
  if (newUrl.indexOf(redOnlyViews[i]) > -1 && type === "blue") {
      $location.path("/" + redOnlyViews[i]).search({type: "red"});
   }
 }

(在$rootScope.$on("$locationChangeStart", function (event, newUrl, oldUrl)函数内部)

如果有一种方法可以更改这样的参数,而又不更改任何其他奇妙的参数!

If there is a way of changing a parameter like this whilst not changing any of the other parameters that would be fantastic!

希望这很有道理,谢谢:)

Hope this makes sense and thanks :)

为了匿名起见,将颜色更改为颜色,我并不生气:)

推荐答案

如果要设置单个搜索参数,则可以

If you want to set a single search parameter you can do

$location.search(angular.extend($location.search(), {type: 'red'}))

这篇关于如果AngularJS与值匹配,则在加载时更改单个URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆