我使用 $location.path() 添加那里的数据@object
,通过提交表单有: url.com/@object
,但是当重新加载页面时 - 找不到页面:我不需要打开另一个页面,我只需要使用此参数重新加载 url.com @object
。怎么可能?
页面的原因很可能是因为没有定义将path
与数据对象匹配的route
。
如果要从 URL 中传递数据,同时仍匹配路由,有几种方法可以做到这一点。
-
$routeParams
https://docs.angularjs.org/api/ngRoute/service/$routeParams
// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
-
$location.hash()
https://docs.angularjs.org/api/ng/service/$location#hash
// given URL http://example.com/#/some/path?foo=bar&baz=xoxo#hashValue
var hash = $location.hash();
// => "hashValue"
-
$location.search()
https://docs.angularjs.org/api/ng/service/$location#search
// given URL http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}
// set foo to 'yipee'
$location.search('foo', 'yipee');
// $location.search() => {foo: 'yipee', baz: 'xoxo'}
但是,最好使用Service
来存储数据。 在AngularJS中,服务是Singleton
的,这意味着它们被实例化一次,非常适合存储state
和/或data
。
有关如何使用服务的更多信息。
如果您有任何问题,请告诉我。