【问题标题】:How to set query string in url using Angular.js?如何使用 Angular.js 在 url 中设置查询字符串?
【发布时间】:2016-03-23 05:44:31
【问题描述】:

我需要在 URL 中的查询字符串中传递一个 id 并使用 Angular.js 设置路径。我在下面添加我的代码。

http://localhost/spesh/resetpass?id=2

对于上面的 url,我需要在下面给出的路由文件中设置路径。

.state('/resetpass', { 
            url: '/resetpass',
            templateUrl: 'dashboardview/resetpass.html',
            controller: 'resetpassController'
})

我按照上面做了,但没有按要求工作。

【问题讨论】:

  • 我认为您的问题应该更具体。您正在努力实现的目标以及到目前为止您已经尝试过的目标是什么。您提供的代码就像路由设置。你知道查询字符串是独立于路由的吗?

标签: javascript angularjs angular-ui-router


【解决方案1】:

这应该更好 -

.state('resetpass', {     
            url: '/resetpass/:id',
            templateUrl: 'dashboardview/resetpass.html',
            controller: 'resetpassController'
})

你可以这样使用它 -

$state.go('resetpass', {id: 1} );

<a ui-sref="resetpass({id:1})"></a>

您可以在 $stateParams 服务中获取查询字符串来做任何您想做的事情。

【讨论】:

    【解决方案2】:

    希望这会有所帮助.. 更改代码如下

    http://localhost/spesh/resetpass/2       /////CHANGE HERE
    
    
    .state('/resetpass', { 
                url: '/resetpass/:id',      /////CHANGE HERE
                templateUrl: 'dashboardview/resetpass.html',
                controller: 'resetpassController'
    })
    
    
    ///to access ID in controller 
    var ID = $stateParams.id;
    

    如果您想保留“?”,试试这个。下面的代码告诉 Angular 需要 id。

    url: "/resetpass?id",
    

    【讨论】:

    • 我需要在 url.any 解决方案中保留?
    【解决方案3】:

    改变它:

    .state('/resetpass', { 
                url: '/resetpass?id',
                templateUrl: 'dashboardview/resetpass.html',
                controller: 'resetpassController'
    })
    

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多