Angular Resource Encoding URL
Asked Answered
B

3

8

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!

Bennir answered 23/5, 2013 at 1:19 Comment(0)
P
9

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

$scope.go = function (params) {
    $location.path(decodeURIComponent(params));
};
Peay answered 23/5, 2013 at 6:24 Comment(0)
M
2

you can also use unescape instead of decodeURIComponent.

Refer below code snippet -

$scope.go = function (params) {
    $location.path(unescape(params));
};
Malvie answered 20/1, 2016 at 8:59 Comment(1)
This solution is deprecated, prefer decodeURIComponent(p)Weinhardt
D
0

I have created a filter in angularJs project to decode the URL. For example if your URL is- http://www.example.com/test1 test2 tes3

Then filter make the URL like this- http://www.example.com/test1-test2-tes3

in my angular project the main app name is angularApp.

var app = angular.module('angularApp', []);// This is your main angular app.

Now you want to create a filter for decode url.

app.filter('decodeURL', function() {
    return function(text) {
        if(text) {
            return text.split(' ').join('-').toLowerCase().replace(/[^a-z0-9]+/g, '-');
        }
    }
});

The above code is to create a filter to decode url. And my filter name is 'decodeURL' . we will use decodeURL as a filter in my code

See the snapshot

How to use this filter in the html-

<a ui-sref="{{business.category[0].categoryName.toLowerCase()}}Detail({id:business.id,title:(business.title | decodeURL)})"></a>

// The above is for state routing in angularjs.

See the snapshot

<a href="/coupon/{{coupon.id}}/{{coupon.title | decodeURL}}"
                                       class="btn btn-warning show-btnhome show-button-margin">Show</a>

//The above code for URL redirecting.

See the snapshot

Dug answered 18/1, 2017 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.