Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在AngularJS中将查询字符串附加到url?_Javascript_Angularjs_Url_Location_Query String - Fatal编程技术网

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

Javascript 如何在AngularJS中将查询字符串附加到url?,javascript,angularjs,url,location,query-string,Javascript,Angularjs,Url,Location,Query String,我是新手。我有一个带有输入文本框的搜索页面来搜索customerId,但我想添加一个功能,在这里我也可以根据url字符串进行搜索 例如,我的url是: http://localhost:8080/#/search 但我需要像这样的东西 http://localhost:8080/#/search?ACC34ff 这样我也可以根据url中的customerId进行搜索 有人能告诉我如何添加查询字符串吗 app.config(['$routeProvider', function($route

我是新手。我有一个带有输入文本框的搜索页面来搜索customerId,但我想添加一个功能,在这里我也可以根据url字符串进行搜索

例如,我的url是:

http://localhost:8080/#/search
但我需要像这样的东西

http://localhost:8080/#/search?ACC34ff 
这样我也可以根据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 >
  • 搜索页面:


    谢谢

    您只需将customerId作为参数传递即可

    .when('/Search', {
            templateUrl: function (params) { return 'partials/search?customer=' + params.CustomerId; }
        })
    
    在以下链接中,还解释了如何在需要时传递多个参数:

    您只需将customerId作为参数传递

    .when('/Search', {
            templateUrl: function (params) { return 'partials/search?customer=' + params.CustomerId; }
        })
    
    在以下链接中,还解释了如何在需要时传递多个参数:

    首先,我不敢相信你没有得到更多的答案,因为有很多方法可以通过url传递和检索变量,每种方法都是两种主要方法的细微变化

  • 作为查询字符串的一部分(根据您的请求)
  • 作为路由参数(也值得一提)
  • 下面的示例假设您有一个客户id的文本输入,如下所示:

    <input type="text" ng-model="customerId" />
    
    <a href="#search?cId={{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    <a href="#search/{{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    现在,当您单击该链接时,您将被带到一个url,例如:

    http://localhost:8080/#/search?cId=ACC34ff 
    
    http://localhost:8080/#/search/ACC34ff
    
    然后,您可以使用服务在控制器中拾取
    cId
    参数

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

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

    app.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider
        .when('/search/:cId', {
            templateUrl: 'partials/search.html',
            controller: 'searchCtrl',
            reloadOnSearch: false
        })
    }]);
    
    /{{customerId}}
    附加到您的链接,如下所示:

    <input type="text" ng-model="customerId" />
    
    <a href="#search?cId={{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    <a href="#search/{{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    您可以使用服务在控制器中拾取
    cId
    参数


    首先,我不敢相信你没有得到更多的答案,因为有很多方法可以通过url传递和检索变量,每种方法都是两种主要方法的细微变化

  • 作为查询字符串的一部分(根据您的请求)
  • 作为路由参数(也值得一提)
  • 下面的示例假设您有一个客户id的文本输入,如下所示:

    <input type="text" ng-model="customerId" />
    
    <a href="#search?cId={{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    <a href="#search/{{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    现在,当您单击该链接时,您将被带到一个url,例如:

    http://localhost:8080/#/search?cId=ACC34ff 
    
    http://localhost:8080/#/search/ACC34ff
    
    然后,您可以使用服务在控制器中拾取
    cId
    参数

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

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

    app.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider
        .when('/search/:cId', {
            templateUrl: 'partials/search.html',
            controller: 'searchCtrl',
            reloadOnSearch: false
        })
    }]);
    
    /{{customerId}}
    附加到您的链接,如下所示:

    <input type="text" ng-model="customerId" />
    
    <a href="#search?cId={{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    <a href="#search/{{ customerId }}" role="tab" data-toggle="tab">Search</a>
    
    您可以使用服务在控制器中拾取
    cId
    参数


    我不需要控制器端的任何东西thn?我不清楚它将如何使用CustomerIdal,当用户使用url本身而不是我的坏url时,我该如何使用路由器。您需要设置控制器以读取参数$http.get('/Search/'+$routeParams.customerId).success(函数(响应){….});我不需要控制器端的任何东西thn?我不清楚它将如何使用CustomerIdal,当用户使用url本身而不是我的坏url时,我该如何使用路由器。您需要设置控制器以读取参数$http.get('/Search/'+$routeParams.customerId).success(函数(响应){….});