My ui-router
optional parameters create a long not so friendly url, how do I shorten the url while persisting my parameters. I need to be able to share this url over social media, be SEO friendly.
If I remove some of the parameters from my url in my state it still goes through to my view displays the correct data, but on refresh only the data from the parameters that I included in my url is displayed.
My link:
<a class = "xtradetails" ng-click = "vm.detailsClick(car)">VIEW DETAILS</a>
JS:
vm.detailsClick = function (car) {
vm.detailsdata = car;
$state.params.car = car;
$state.go("cardetails", {
car : car,
stock_id : car.stock_id,
make : car.make,
year : car.year,
mileage : car.mileage,
variant : car.variant,
selling_price: car.selling_price,
colour : car.colour,
condition : car.condition,
branch : car.branch,
extras_csv : car.extras_csv,
description : car.description,
location : car.location,
body_type : car.body_type,
province : car.province,
company_id : car.company_id,
url1 : car.url1,
url2 : car.url2,
url3 : car.url3,
url4 : car.url4,
url5 : car.url5,
url6 : car.url6,
url7 : car.url7,
url8 : car.url8,
url9 : car.url9,
url10 : car.url10
}, {});
};
Here is my state:
.state("cardetails", {
params : {
car : null,
make : null,
stock_id : null,
company_id : null,
year : null,
selling_price: null,
mileage : null,
variant : null,
colour : null,
condition : null,
branch : null,
extras_csv : null,
description : null,
province : null,
contact_tel : null,
url1 : null,
url2 : null,
url3 : null,
url4 : null,
url5 : null,
url6 : null,
url7 : null,
url8 : null,
url9 : null,
url10 : null,
squash : true
},
templateUrl: "partials/cardetails.html",
url : "/:make/:stock_id/:year/:selling_price/:mileage/:variant/:colour/:condition/:location/:province/:body_type/:branch/:extras_csv/:description/:url1/:url2/:url3/:url4/:url5/:url6/:url7/:url8/:url9/:url10",
controller : "Details"
})
My controller:
app.controller('Details', ["$scope", "$stateParams", function ($scope, $stateParams) {
$scope.car = $stateParams.car;
$scope.stock_id = $stateParams.stock_id;
$scope.make = $stateParams.make;
$scope.year = $stateParams.year;
$scope.variant = $stateParams.variant;
$scope.mileage = $stateParams.mileage;
$scope.colour = $stateParams.colour;
$scope.condition = $stateParams.condition;
$scope.selling_price = $stateParams.selling_price;
$scope.branch = $stateParams.branch;
$scope.extras_csv = $stateParams.extras_csv;
$scope.description = $stateParams.description;
$scope.location = $stateParams.location;
$scope.body_type = $stateParams.body_type;
$scope.company_id = $stateParams.company_id;
$scope.contact_number = $stateParams.contact_number;
$scope.address = $stateParams.address;
$scope.dealer = $stateParams.dealer;
$scope.suburb = $stateParams.suburb;
$scope.province = $stateParams.province;
$scope.contact_tel = $stateParams.contact_tel;
$scope.name = $stateParams.name;
$scope.url1 = $stateParams.url1;
$scope.url2 = $stateParams.url2;
$scope.url3 = $stateParams.url3;
$scope.url4 = $stateParams.url4;
$scope.url5 = $stateParams.url5;
$scope.url6 = $stateParams.url6;
$scope.url7 = $stateParams.url7;
$scope.url8 = $stateParams.url8;
$scope.url9 = $stateParams.url9;
$scope.url10 = $stateParams.url10;
$scope.car = {
make : $scope.make,
stock_id : $scope.stock_id,
company_id : $scope.company_id,
dealer : $scope.dealer,
year : $scope.year,
variant : $scope.variant,
mileage : $scope.mileage,
selling_price : $scope.selling_price,
colour : $scope.colour,
condition : $scope.condition,
branch : $scope.branch,
extras_csv : $scope.extras_csv,
description : $scope.description,
location : $scope.location,
body_type : $scope.body_type,
contact_number: $scope.contact_number,
address : $scope.address,
suburb : $scope.suburb,
province : $scope.province,
contact_tel : $scope.contact_tel,
name : $scope.name,
url1 : $scope.url1,
url2 : $scope.url2,
url3 : $scope.url3,
url4 : $scope.url4,
url5 : $scope.url5,
url6 : $scope.url6,
url7 : $scope.url7,
url8 : $scope.url8,
url9 : $scope.url9,
url10 : $scope.url10
};
}]);
2
Answers
I found a solution that worked for me, show all the details with only the parameters i need in my url, while persisting on refresh. Maybe some else would benefit from this.
Function:
My state:
My Controller:
In order for your application to work correctly, you need all of your parameters be to present in the URL. Now if you wish to have a more appealing URL to share on social media, there are several online services for URL shortening that you can use.
I suggest you use the google URL shortener. For example this question has a relatively long URL:
Which becomes after shortening:
NB: please note that I have added inverted slashes to the URLs to avoid having them interpreted as clickable links.