Angular Resource URL

I have a resource defined as follows:

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

In my opinion, 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 subject in question, d.param is equal to

 TkZUOWZwcnc9Uldo%0ASzRvd2FiWk 

But when I call DatumItem.get () with the correct ID, it changes the identifier to

 TkZUOWZwcnc9Uldo%250ASzRvd2FiWk 

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

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

Any help would be greatly appreciated, thanks!

+8
angularjs urlencode angular-resource
source share
3 answers

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

 $scope.go = function (params) { $location.path(decodeURIComponent(params)); }; 
+9
source share

you can also use unescape instead of decodeURIComponent.

See code snippet below -

 $scope.go = function (params) { $location.path(unescape(params)); }; 
+1
source share

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

Then the filter will make the URL as follows: http://www.example.com/test1-test2-tes3

in my angular project, the main application name is angularApp.

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

Now you want to create a filter to decrypt the URL.

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

The above code is for creating a filter to decode a URL. And my filter name is "decodeURL". we will use decodeURL as a filter in my code

See snapshot

How to use this filter in html -

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

// The above applies to state routing in angular mode.

View snapshot

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

// The above URL redirect code.

View snapshot

0
source share

All Articles