gpt4 book ai didi

angularjs - angularjs中的多个查询字符串参数

转载 作者:行者123 更新时间:2023-12-04 10:00:04 29 4
gpt4 key购买 nike

我正在努力传递和读取路由中的多个查询字符串参数。

$routeProvider.when("/joboffers:keywords:location", {
controller: "jobOffersController",
templateUrl: "/App/Views/JobOffer/All.html"
});

这是搜索页面:
$scope.searchJobOffer = function () {
var vm = $scope.jobOfferSearchViewModel;
var path = "/joboffers?keywords=" +( vm.keywords || "") + "&location=" + (vm.location || "");
$location.path(path);
}

这是JobOffersController:
'use strict';
app.controller('jobOffersController', ['$scope', '$routeParams', 'jobOfferService', function ($scope, $routeParams, jobOfferService) {
$scope.jobOffers = [];

function init() {
var keywords = $routeParams.keywords;
var location = $routeParams.location;
}

init();
}]);

读取$ routeParams根本不起作用。如果我将“developer”作为关键字,将“New York”作为位置,则$ routeParam对象将如下所示:
{keywords: "?keywords=developer&location=New Yor", location: "k"}

有人可以告诉我我做错了什么吗?提前致谢。

P.S.
这可能是由于路由配置错误导致的吗?
当我通过 searchJobOffer函数导航时,它会将URL编码为: http://localhost:49380/#/joboffers%3Fkeywords=developer&location=london,如果我尝试使用此URL http://localhost:49380/#/joboffers?keywords=developer&location=london,则路由系统会将我引导至默认路由(#/home)

最佳答案

$ routeProvider与查询字符串不匹配,仅与路由匹配。另外,您将完整的url设置为$ location.path(),而$ location.path()仅采用url的path部分。要设置包括查询字符串在内的整个URL,您需要使用$location.url()

以下是一些选择:

1.改用漂亮的网址

$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();
}]);

2.仅在工作上进行匹配才能提供路径,并从$ location.search()中提取参数

(请注意,使用 $location.url()而不是 $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();
}]);

3.如果您需要匹配路由和查询字符串,请尝试使用更强大的功能,例如 angular-ui-router
$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();
}]);

关于angularjs - angularjs中的多个查询字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26692747/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com