Edward Tanguay
Edward Tanguay

Reputation: 193282

How to change url variables with $location?

I am trying to get code to work at jsfiddle which reads and changes the url variables in the url:

http://jsfiddle.net/edwardtanguay/3k4j4mz5/3

'use strict';
angular.module('myApp', [])
    .controller('mainController', function ($scope, $location, $log) {

    $scope.changeUrl = function() {
        var path = $location.path();
        var id =Math.floor(Math.random() * 60) + 20;
        $location.url(path + '?customer=' + id);
        $log.info('path='+path);
        $log.info('id='+id);
    };

});

Why will the above code not add e.g. ?customer=23 at the end of the URL? Is the code not correct or is this a limitation of jsfiddle?

Upvotes: 1

Views: 66

Answers (2)

justtry
justtry

Reputation: 639

Your example should work, but that is not possible in jsfiddle. So, yes this is a limitation of jsfiddle.

Upvotes: 2

MoLow
MoLow

Reputation: 3084

from the $location Developer guide:

$location service provides setter methods for url, path, search, hash

so you could change query string like this:

var id = Math.floor(Math.random() * 60) + 20;
$location.search({customer: id})

Upvotes: 1

Related Questions