Your <select> element will be bound to a model with ng-model , which you can $watch and use to update either $location.path or $location.search . Personally, I would suggest using $location.search : you can just change the parameter you want, and it works a little less, since you do not need to know the whole path in your controller.
So, suppose you have a <select> element:
<select ng-model="selectedColor" ng-options="color for color in colors">
You can use $watch to view your associated value and update your $location.search by pointing it to null if the color is undefined or otherwise false (this clears the search parameter):
$scope.$watch('selectedColor', function (color) { if (color) { $location.search('color', color); } else { $location.search('color', null); } });
You might want to establish a two-way binding between the search parameter and the local model so that the changes are reflected in your <select> :
$scope.$watch('$location.search().color', function (color) { $scope.selectedColor = color; });
Then it's just a matter of accessing $routeParams.color in your routed controller.
See this plunk for a complete working example.
Scott baldwin
source share