如何使用AngularJS Route仅检测查询字符串参数的变化?

时间:2022-08-24 12:54:53

I wanted to work with query string parameters along with the usual route parameters in an AngularJS application and have been following the solution in this post: how to pass querystring in angular routes?

我想在AngularJS应用程序中使用查询字符串参数以及通常的路由参数,并且已经在这篇文章中遵循解决方案:如何在角度路由中传递查询字符串?

The solution seems to work: Change the route first with $location.path and then apply the query string parameters with $location.search.

该解决方案似乎有效:首先使用$ location.path更改路由,然后使用$ location.search应用查询字符串参数。

However, I came across a scenario where the route does not need to change but the query parameters need to.

但是,我遇到了一个方案,其中路由不需要更改,但查询参数需要。

For example:

例如:

Moving from
'/Books/Programming?id=123'
to
'/Books/Mathematics?id=234'
works fine.

从'/ Books / Programming?id = 123'移动到'/ Books / Mathematics?id = 234'工作正常。

However,

然而,

Moving from
'Books/Programming?id=123'
to
'Books/Programming?id=124'
does not work.

从'Books / Programming?id = 123'移动到'Books / Programming?id = 124'不起作用。

It seems that AngularJS is only notified for changes on the regular route parameters and not for changes in the query string parameters so if query string is the only thing that changes in the URL, the application does not respond to the navigation.

似乎AngularJS仅通知常规路由参数的更改而不是更改查询字符串参数,因此如果查询字符串是URL中唯一更改的内容,则应用程序不响应导航。

1 个解决方案

#1


0  

As verified with AngularJS 1.6.5, and according to the Plunkr by @JB-Nizet, the problem does not occur anymore. The question still remains for what I missed earlier that it did not work.

正如使用AngularJS 1.6.5验证的那样,根据@ JB-Nizet的Plunkr,问题不再发生。对于我之前错过的它不起作用的问题仍然存在。

Marking it as resolved as it does not occur anymore.

将其标记为已解决,因为它不再发生。

#1


0  

As verified with AngularJS 1.6.5, and according to the Plunkr by @JB-Nizet, the problem does not occur anymore. The question still remains for what I missed earlier that it did not work.

正如使用AngularJS 1.6.5验证的那样,根据@ JB-Nizet的Plunkr,问题不再发生。对于我之前错过的它不起作用的问题仍然存在。

Marking it as resolved as it does not occur anymore.

将其标记为已解决,因为它不再发生。