I\'m struggling with passing and reading multiple query string parameters in a route.
$routeProvider.when(\"/joboffers:keywords:location\", {
controller:
$routeProvider does not match on query strings, only routes. Also, you're setting a full url
to $location.path() and $location.path() only takes the path
piece of the url. To set the entire URL including query string you need to use $location.url()
.
Here are a few options:
$routeProvider.when("/joboffers/:location/:keywords", {
controller: "jobOffersController",
templateUrl: "/App/Views/JobOffer/All.html"
});
$scope.searchJobOffer = function () {
var vm = $scope.jobOfferSearchViewModel;
var path = "/joboffers/" + (vm.location || "") + "/" + ( vm.keywords || "");
$location.path(path);
};
app.controller('jobOffersController', ['$scope', '$routeParams', 'jobOfferService', function ($scope, $routeParams, jobOfferService) {
$scope.jobOffers = [];
function init() {
var keywords = $routeParams.keywords;
var location = $routeParams.location;
}
init();
}]);
(note the use of $location.url()
instead of $location.path()
)
$routeProvider.when("/joboffers", {
controller: "jobOffersController",
templateUrl: "/App/Views/JobOffer/All.html"
});
$scope.searchJobOffer = function () {
var vm = $scope.jobOfferSearchViewModel;
var url = "/joboffers?keywords=" +( vm.keywords || "") + "&location=" + (vm.location || "");
$location.url(url);
};
app.controller('jobOffersController', ['$scope', '$location', 'jobOfferService', function ($scope, $location, jobOfferService) {
$scope.jobOffers = [];
function init() {
var search = $location.search();
var keywords = search.keywords;
var location = search.location;
}
init();
}]);
$stateProvider.state("JobOffers", {
url: '/joboffers?keywords&location',
controller: "jobOffersController",
templateUrl: "/App/Views/JobOffer/All.html"
});
$scope.searchJobOffer = function () {
var vm = $scope.jobOfferSearchViewModel;
var url = "/joboffers?keywords=" +( vm.keywords || "") + "&location=" + (vm.location || "");
$location.url(url);
};
app.controller('jobOffersController', ['$scope', '$stateParams', 'jobOfferService', function ($scope, $stateParams, jobOfferService) {
$scope.jobOffers = [];
function init() {
var keywords = $stateParams.keywords;
var location = $stateParams.location;
}
init();
}]);