我如何可以设置AngularJS查询参数没有做路由? [英] How can I set a query parameter in AngularJS without doing a route?

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

问题描述

我的角度应用程序允许用户加载的项目,而当这种情况发生,我想设置包含该项目的ID查询字符串,这样如果用户刷新页面(或想要链接到它),在网址已被设置(有已经code。在加载项如果ID在$ routeParams present的地方)。

My Angular app allows the user to load an item, and when that happens I'd like to set a query string that contains the item's Id so that if the user refreshes the page (or wants to link to it), the URL is already set (there's already code in place that loads the item if the Id is present in $routeParams).

如何设置,而不会造成路由查询参数?有页面上的其他事情,将被重置(包括一个插件,将不得不重新加载所有数据),如果路由发生,因此,重要的是刚刚的查询参数的变化。

How can I set that query parameter without causing a route? There are other things on the page that will get reset (including a plugin that will have to reload all of its data) if a route happens, so it's important that just the query parameter changes.

总之,当我加载项目123,所有我想发生的是从URL变化:

In short, when I load item 123, all I want to happen is that the URL changes from:

www.myurl.com /#/项目

www.myurl.com/#/Items

www.myurl.com /#/项目?ID = 123

www.myurl.com/#/Items?id=123

无需任何布线的发生。

什么是做到这一点的最好方法是什么?

What's the best way to do this?

推荐答案

在您 $ routeProvider 配置集 reloadOnSearch 为false:

In your $routeProvider configuration set reloadOnSearch to false:

$routeProvider
  .when('/items', {
    controller: 'ItemsCtrl',
    templateUrl: '/templates/items',
    reloadOnSearch: false
  },
  ...
);

和在你的控制器使用 $ location.search()设置 ID 参数:

and in your controller use $location.search() to set the id param:

$location.search('id', 123);

这篇关于我如何可以设置AngularJS查询参数没有做路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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