【问题标题】:Not linking another page by path in AngularJS不在AngularJS中通过路径链接另一个页面
【发布时间】:2017-06-06 20:35:00
【问题描述】:

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

【问题讨论】:

    标签: javascript angularjs routing single-page-application


    【解决方案1】:

    找不到您的页面的原因很可能是因为没有定义与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 中,服务是 Singletons,这意味着它们被实例化一次,并且非常适合存储 state 和/或 data

    More info on how to use services.

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

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2011-08-06
      • 1970-01-01
      • 2013-11-02
      • 2010-11-18
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多