Getting values from query string in an url using AngularJS $location

后端 未结 9 951
鱼传尺愫
鱼传尺愫 2020-11-27 04:36

Regarding $location.search, the docs say,

Return search part (as object) of current url when called without any parameter.

In my

9条回答
  •  隐瞒了意图╮
    2020-11-27 05:02

    My fix is more simple, create a factory, and implement as one variable. For example

    angular.module('myApp', [])
    
    // This a searchCustom factory. Copy the factory and implement in the controller
    .factory("searchCustom", function($http,$log){    
        return {
            valuesParams : function(params){
                paramsResult = [];
                params = params.replace('(', '').replace(')','').split("&");
                
                for(x in params){
                    paramsKeyTmp = params[x].split("=");
                    
                    // Si el parametro esta disponible anexamos al vector paramResult
                    if (paramsKeyTmp[1] !== '' && paramsKeyTmp[1] !== ' ' && 
                        paramsKeyTmp[1] !== null){          
                        
                        paramsResult.push(params[x]);
                    }
                }
                
                return paramsResult;
            }
        }
    })
    
    .controller("SearchController", function($scope, $http,$routeParams,$log,searchCustom){
        $ctrl = this;
        
        var valueParams = searchCustom.valuesParams($routeParams.value);
        valueParams = valueParams.join('&');
    
        $http({
            method : "GET",
            url: webservice+"q?"+valueParams
        }).then( function successCallback(response){
            data = response.data;
            $scope.cantEncontrados = data.length; 
            $scope.dataSearch = data;
            
        } , function errorCallback(response){
            console.log(response.statusText);
        })
          
    })
    
    
    
    
    
    

提交回复
热议问题