Seglespaan Seglespaan - 5 months ago 36
AngularJS Question

Angular, using select input filter to update route?

I have my app set up where a list of products can be filtered by colour using a select input, I also have the

$routeprovider
passing this colour param to the page if it is present in the url.

What I want to do now is update the url / route when the select box is changed. How do I bind the change of the select to the route?

Answer

select has an undocumented ng-change parameter that you can use to call a function to set $location.path:

<select ... ng-model="color" ng-change="updatePath()">

Controller:

function MyCtrl($scope, $location) {
    $scope.updatePath = function() {
       $location.path(... use $scope.color here ...);
    }
}
Comments