[英]angularjs $location and window.history.pushState
The URL for my angular app is something like this: 我的角度应用程序的URL是这样的:
http://www.domain.com/project/edit/0#/step1 //create new project if project_id = 0
^
http://www.domain.com/project/edit/1#/step1 //edit existing project if project_id > 0
^
http://www.domain.com/project/edit/2#/step1 //edit existing project if project_id > 0
^
When the user saves the project and the project_id is 0, i basically just call a server-side script and it returns the project_id of the newly created project (say, "3"). 当用户保存项目并且project_id为0时,我基本上只是调用服务器端脚本,并且它将返回新创建的项目的project_id(例如,“ 3”)。 I then update the URL in address bar with the project_id returned by script as follows: 然后,我用脚本返回的project_id更新地址栏中的URL,如下所示:
window.history.pushState({}, "URL updated", '/project/edit/'+new_project_id);
This would change the URL in the address bar without re-loading the page (so if the user pressed save, the URL in address bar would automatically change to http://www.domain.com/project/edit/3
which is also the URL to edit project later). 这将更改地址栏中的URL,而无需重新加载页面(因此,如果用户按下“保存”,地址栏中的URL将自动更改为http://www.domain.com/project/edit/3
,这也是URL,以便以后编辑项目)。
But recently I've added $routeProvider
to my app (I'm using the hash syntax #/step1
, #/step2
, #/step3
at the end of URL to manage views). 但是最近我在应用程序中添加了$routeProvider
(我在URL末尾使用哈希语法#/step1
, #/step2
, #/step3
来管理视图)。
Now I get this infidig
error whenever I call window.history.pushState
and also the $routeProvider
just redirects / reloads the page to http://www.domain.com/project/edit/0#/step1
instead of changing the URL of address bar. 现在,每当我调用window.history.pushState
时,我都会收到此infidig
错误,并且$routeProvider
只是将页面重定向/重新加载到http://www.domain.com/project/edit/0#/step1
而不是更改URL地址栏。
I've tried using $location.path
to update my URL, but that changes the part after the hash sign ( #/step1
part) while I want to change the part before the #
sign ( /edit/0
to /edit/4
, etc) 我尝试使用$location.path
更新我的URL,但这会更改井号( #/step1
部分)之后的部分,而我想更改#
号之前的部分( /edit/0
到/edit/4
等)
So my question is how do I change the URL in address bar like window.history.pushState
using $location
or $route
? 所以我的问题是如何使用$location
或$route
更改地址栏(例如window.history.pushState
的URL?
To start, I would recommend that you checkout ui-router as it has much improved functionality over angular's built in route provider, such as state management and nested views. 首先,我建议您检出ui-router,因为它比angular的内置路由提供程序具有更好的功能,例如状态管理和嵌套视图。
Secondly, you really don't change the URL structure before the hash. 其次,您实际上不需要更改哈希之前的URL结构。 The hash (if using this method) should be built off your root url for angular, so http://www.domain.com/#/
. 哈希(如果使用此方法)应在您的根URL角度基础上构建,因此http://www.domain.com/#/
。 Then in your case you would have http://www.domain.com/#/project/edit/1
那么在您的情况下,您将拥有http://www.domain.com/#/project/edit/1
Using ui-router, you create states
, which are essentially routes. 使用ui-router,您可以创建states
,它们实际上是路由。 For example, you may create one like: 例如,您可以创建一个类似于:
$stateProvider .state('project_edit', { url: "/project/edit/:id", templateUrl: "project.tpl.html", controller: ProjectEditController }) })
In your controller or service after you make your request and receive your project id, you would simply call the $state
service using $state.go('project_edit', {id : 1})
and you'll be taken to the ProjectEditController
controller where id
will be available. 在您发出请求并收到项目ID后,在您的控制器或服务中,您只需使用$state.go('project_edit', {id : 1})
调用$state
服务,您将被带到ProjectEditController
控制器哪里有id
。
I'd recommend reading the docs for ui-router and go through some of the angular docs as well. 我建议阅读ui-router的文档,并阅读一些角度文档。 It sounds like you're trying to co-mingle traditional push state usage and angular usage. 听起来您正在尝试将传统的推送状态用法和角度用法混为一谈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.