【问题标题】:How to append query string to url in AngularJS?如何在AngularJS中将查询字符串附加到url?
【发布时间】:2017-06-29 07:09:38
【问题描述】:

我是 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 >

搜索页面:

谢谢

【问题讨论】:

    标签: javascript angularjs url location query-string


    【解决方案1】:

    首先,我不敢相信您没有得到更多答案,因为有很多方法可以通过 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>
    

    现在,当您点击链接时,您将被带到如下网址:

    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>
    

    现在,当您点击链接时,您将被带到如下网址:

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

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

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

    【讨论】:

      【解决方案2】:

      您只需将 customerId 作为参数传递。

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

      在下面的链接中,还有关于如何传递多个参数的说明,以备不时之需: https://stackoverflow.com/a/35967896/5988277

      【讨论】:

      • 我不需要控制器端的任何东西吗?我不清楚它将如何使用 customerID
      • 当用户使用 url 本身时,我如何路由 localhost:8080/#/search?ACC34ff 而不是 localhost:8080/#/search
      • 我的错。您需要设置控制器以读取参数。 $http.get('/Search/'+$routeParams.customerId) .success(function (response) { .... });
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多