在 AngularJS 中不按路径链接另一个页面



我使用 $location.path() 添加那里的数据@object,通过提交表单有: url.com/@object ,但是当重新加载页面时 - 找不到页面:我不需要打开另一个页面,我只需要使用此参数重新加载 url.com @object。怎么可能?

找不到

页面的原因很可能是因为没有定义将path与数据对象匹配的route

如果要从 URL 中传递数据,同时仍匹配路由,有几种方法可以做到这一点。

  1. $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'}
  1. $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"
  1. $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

有关如何使用服务的更多信息。

如果您有任何问题,请告诉我。

最新更新