Angular Resource Encoding URL

24,114

Solution 1

Since the URL is already URIencoded you need to decode it before passing it to angular:

$scope.go = function (params) {
    $location.path(decodeURIComponent(params));
};

Solution 2

you can also use unescape instead of decodeURIComponent.

Refer below code snippet -

$scope.go = function (params) {
    $location.path(unescape(params));
};
Share:
24,114
Nader Hendawi
Author by

Nader Hendawi

Updated on July 09, 2022

Comments

  • Nader Hendawi
    Nader Hendawi almost 2 years

    I have a resource defined as follows:

    app.factory("DatumItem", function($resource) {
        return $resource('/data/:id', {id: '@id'});
    });
    

    In my view I have:

    <div ng-click="go('/datum/' + d.to_param)">Test</div>
    

    where go() is defined in my controller as:

    $scope.go = function (params) {
        $location.path(params);
    };
    

    For the item in question, d.param is equal to

    TkZUOWZwcnc9Uldo%0ASzRvd2FiWk
    

    But when I call DatumItem.get() with the correct ID, it is changing the id to

    TkZUOWZwcnc9Uldo%250ASzRvd2FiWk
    

    Is there a way to prevent the % from being encoded to a %25 in this case?

    I've tried a combination of using encodeURI, encodeURIComponent to no avail.

    any help would be greatly appreciated, thanks!

  • Leogout
    Leogout about 8 years
    This solution is deprecated, prefer decodeURIComponent(p)