Angular Resource Encoding URL

拟墨画扇 提交于 2019-12-04 03:54:59

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

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

you can also use unescape instead of decodeURIComponent.

Refer below code snippet -

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

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

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.

<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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!