繁体   English   中英

如何在AngularJS中将查询字符串附加到url?

[英]How to append query string to url in AngularJS?

我是angular js的新手。 我有带有输入文本框的搜索页面,用于搜索customerId,但是我想添加一个功能,使我也可以基于url字符串进行搜索。

例如,我的网址是:

http://localhost:8080/#/search

但是我需要这样的东西

http://localhost:8080/#/search?ACC34ff 

http:// localhost:8080 /#/ search?CustomerId,以便我也可以基于url中的customerId搜索

有人可以告诉我如何添加查询字符串吗?

app.config(['$routeProvider',
function($routeProvider) {
    $routeProvider
    .when('/home', {
        templateUrl: 'partials/home.html',
        controller: 'homePageCtrl',
        reloadOnSearch: true
    }).when('/features', {
        templateUrl: 'partials/features.html',
        controller: 'featuresCtrl',
        reloadOnSearch: false
    }).when('/search', {
        templateUrl: 'partials/search.html',
        controller: 'searchCtrl',
        reloadOnSearch: false
    }).otherwise({
        redirectTo: '/home'
    });
}]);

控制器:

appControllers.controller('searchCtrl', ['$scope','$route','$filter', '$http', '$location','$window','$timeout',
    function($scope, $route, $filter, $http, $location, $window, $timeout) {

        $scope.searchCustomerFeatures = function () {

            if($scope.customerId != null) {

            $http.get('/getCustomerFeatures.do', {params: {'customerId': $scope.customerId}}).success(function (data) {

                $scope.featuresJson = angular.toJson(data, true);
                });
            }
        }
    }]);

HTML:

  <li ng-class="{active: isActive('/search')}" style="margin-left: 100px; text-decoration-color: black; font-size: 20px">
            <a href="#search" role="tab" data-toggle="tab">Search</a>
        </li >

搜索页面:

谢谢

首先,我无法相信您没有更多的答案,因为有很多方法可以通过URL传递和检索变量,每种方法都是2种主要方法的细微变化。

  1. 作为查询字符串的一部分(根据您的请求)
  2. 作为路由参数 (也值得一提)

后续示例假定您具有客户id的文本输入,如下所示:

<input type="text" ng-model="customerId" />

1)如何将customerId作为查询字符串的一部分传递

?cId={{ customerId }}附加到您的链接,如下所示:

<a href="#search?cId={{ customerId }}" role="tab" data-toggle="tab">Search</a>

现在,当您单击链接时,您将被带到一个URL,例如:

http://localhost:8080/#/search?cId=ACC34ff 

然后,您可以使用$location服务在控制器中选择cId参数。

app.controller('searchCtrl', function($scope, $location){
    var qsCustomerId = $location.search().cId;
    // Do what you want with qsCustomerId here... 
});

2)如何将customerId传递为路由参数

创建一个指定新的cId路由参数的新路由:

app.config(['$routeProvider',
function($routeProvider) {
    $routeProvider
    .when('/search/:cId', {
        templateUrl: 'partials/search.html',
        controller: 'searchCtrl',
        reloadOnSearch: false
    })
}]);

/{{ customerId }}附加到您的链接,如下所示:

<a href="#search/{{ customerId }}" role="tab" data-toggle="tab">Search</a>

现在,当您单击链接时,您将被带到一个URL,例如:

http://localhost:8080/#/search/ACC34ff

您可以使用$routeParams服务在控制器中选择cId参数。

app.controller('searchCtrl', function($scope, $routeParmas){
    var rpCustomerId = $routeParmas.cId;
    // Do what you want with rpCustomerId here... 
});

您需要做的就是传递customerId作为参数。

.when('/Search', {
        templateUrl: function (params) { return 'partials/search?customer=' + params.CustomerId; }
    })

在以下链接中,还提供了有关如何传递多个参数的说明(如果需要): https : //stackoverflow.com/a/35967896/5988277

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM