Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change params in url with AngularJS?

I want change the url in my angularjs app on the fly by tipping in a inputfield.

e.g.

  1. I stay on: http://localhost/test/year/2012
  2. I will change on the side via a input field the year to 2013 that call my yearIsChanged function, than the url should changed to http://localhost/test/year/2013
  3. But with my current configuration the url is changed to http://localhost/test/year/2012/?year=2013

My modul configuration.

var module = angular.module('exampleApp').
config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when('/test/year/:year', {templateUrl: 'partials/test.html', controller: OverviewCtrl, reloadOnSearch: false}).
        otherwise({redirectTo: '/'});
}]);

Controller action:

 function OverviewCtrl($scope,$routeParams, $location) {
      $scope.yearIsChanged = function () {
        $location.search('year', $scope.year);
    }   
}
like image 226
Silver52 Avatar asked Jan 05 '13 17:01

Silver52


1 Answers

$location.search will create an url like:

http://localhost/test/year/2012?year=2013

which is not what you want. You need to use $location.url():

function OverviewCtrl($scope,$routeParams, $location) {
      $scope.yearIsChanged = function () {
        $location.url('/test/year/' + $scope.year);
    }   
}
like image 54
asgoth Avatar answered Oct 23 '22 09:10

asgoth