简体   繁体   English

使用查询字符串参数过滤数据

[英]Filter data using query string parameters

I have 2 files: 我有2个文件:

module.js with the following code: 使用以下代码的module.js:

angular.module('App.fragments.Jobs', []).
config(['$stateProvider', function($stateProvider) {
    $stateProvider
        .state('jobs', {
            url: '/trabajos',
            templateUrl: '/templates/jobs/list.html',
            controller: 'JobsListCtrl'
        });
}]);

jobs-list-ctrl.js with the following code: 具有以下代码的Jobs-list-ctrl.js:

angular.module('App.fragments.Jobs')
.controller('JobsListCtrl', ['$scope', 'JobsListService', function ($scope, JobsListService) {
    $scope.filter = function() {
        JobsListService.loadJobs($scope.filters, function(_response) {
            console.log(_response.length, "jobs...");
        });
    };

    $scope.filter();
}]);

I'm accessing this through lean.domain.com/apps/hub/noop#/trabajos 我正在通过lean.domain.com/apps/hub/noop#/trabajos访问此文件

The problem is that I want to pass params through my url like this: 问题是我想像这样通过我的网址传递参数:

I'm accessing this through lean.domain.com/apps/hub/noop#/trabajos?param_one=value_one&param_two=value_two... 我正在通过lean.domain.com/apps/hub/noop#/trabajos?param_one=value_one&param_two=value_two ...

and then, take those values in my controller to filter data using the filter method shown before. 然后在我的控制器中获取这些值,以使用前面显示的过滤方法过滤数据。 Something like this: 像这样:

angular.module('App.fragments.Jobs')
.controller('JobsListCtrl', ['$scope', 'JobsListService', function ($scope, JobsListService) {

    //Parse value_one&param_two=value_two from url
    //and fill $scope.filters with value like
    //{param_one: value_one, param_two: value_two}

    $scope.filter = function() {
        JobsListService.loadJobs($scope.filters, function(_response) {
            console.log(_response.length, "jobs...");
        });
    };

    $scope.filter();
}]);

I read about $location and ui-router and $stateParams but I'm not sure what is the best way to do this and why. 我读到有关$ locationui-router以及$ stateParams的信息,但是我不确定执行此操作的最佳方法是什么,为什么?

If you want to extract what's in your query string you can you $location like this: 如果要提取查询字符串中的内容,可以这样定位$ location:

for this url lean.domain.com/apps/hub/noop#/trabajos?param_one=value_one&param_two=value_two, you can use $location.search() that returns the query string in JavaScript object format. 对于此网址lean.domain.com/apps/hub/noop#/trabajos?param_one=value_one&param_two=value_two,您可以使用$ location.search()以JavaScript对象格式返回查询字符串。

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

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