我可以设置使用ngRoute参数? [英] Can I set a parameter using ngRoute?

查看:436
本文介绍了我可以设置使用ngRoute参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出一个应用程序,显示的项目的列表,并且该列表然后可以通过多个过滤器加以改进。如果我是获取从URL字符串的选项列表,那么这将让游客分享(或书签)链接,将您对数据的过滤列表。但我的问题是如何将我写的UR​​L字符串这些选项?

I am making an app that shows a list of items, and that list can then be refined by multiple filters. If I were to fetch a list of those options from the URL string, then that would allow visitors to share (or bookmark) a link that takes you to a filtered list of data. But my questions is how would I write those options to the URL string?

这样的想法是,一​​旦一个选择元件已被改变,以改进结果。比方说,例如,它的项目只是顺序。

So the idea is that once a select element has been changed, to refine the results. Let's say for example that it's just the order of the items.

<select ng-model="order" ng-change="changeOrder()">
  <option ng-repeat="['date', 'amplitude', 'name'] as option">{{ option }}</option>
</select>

我想编写一个选项为URL字符串,因此它现在包含(如果你选择振幅)?为了=振幅。现在,如果您刷新页面,数据很容易被再次幅度排序。

I would want to write that option into the URL string, so that it now contains (if you've selected amplitude) ?order=amplitude. Now, if you refresh the page, the data can easily be sorted by amplitude again.

但是,当有大量的过滤器和排序选项,他们既可以是设置或重置为缺省,变得相当困难把这些选择一起,以检查是否一个选项已经是串,以便在不要把它两次,是否将其后面加一个 如果没有其他选项,或&放大器; 如果有已经定义其他的选择。

But when there is a lot of those filters and sorting options, and they can be both set or reset to default, it becomes rather difficult to put those options all together, to check if an option is already in the string so as to not have it twice, and whether to add it behind a ? if there are no other options, or a & if there are other options already defined.

所以,换句话说,我想知道的是它 ngRoute 提供方法来设置参数;不只是读?所以我可以做类似 $ routeParams.set(订单,振幅)

So in other words, what I want to know is does ngRoute provide methods to set parameters; not just read them? So I could do something like $routeParams.set("order", "amplitude").

推荐答案

如果您想要更改URL,你应该通过的 $位置 服务。

If you want to change the URL, you should do this through the $location service.

具体的 $ location.search(搜索,paramValue)将允许您更改查询字符串值。

Specifically the $location.search(search, paramValue) will allow you to change the query string values.

我做了演示都使用HTML5模式和不断变化的使用这些功能的$位置普拉克。 它可以在这里找到。为了看到URL更改,请点击弹出按钮

I made a plunk that demonstrates both using HTML5 mode and changing the $location using these functions. .

您不必做字符串解析自己。 AngularJS也将允许你(具有相同的功能)来查询当前设置的参数。因此,它查询所有的URL值,改变了一个你改变哪个过滤器,然后设置值恢复的问题。

You don't have to do the string parsing yourself. AngularJS will also allow you (with the same function) to query the currently set parameters. So it's a matter of querying all the URL values, changing the one for which filter you're changing, and then setting the values back.

这是什么的例子中的第二个按钮演示。尽管第一个按钮只能将自身的价值(并删除等),第二个按钮将保持所有其他项目也是如此。

This is what the second button in the example demonstrates. Even though the first button will only set its own value (and remove the others), the second button will keep all other items as well.

相关code:

var values = $location.search();
values.orderThatsUpdating = newOrderValue;
$location.search(values);

这篇关于我可以设置使用ngRoute参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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