如何在AngularJS应用程序中解码URL的部分查询字符串?

时间:2015-01-16 12:50:30

标签: javascript c# angularjs

我有这段代码:

var search = $location.search();
    if (angular.isDefined(search.load) && search.load != null) {
        if (search.load = "confirmEmail")
            authService.confirmEmailUserId = search.userId;
            authService.confirmEmailCode = search.code;
            $state.transitionTo("auth.content", {
                content: search.load
            });
    }

它位于app.run中,它会查看用于打开应用的网址。

但是search.code是用C#编码的

 var callbackUrl  = "http://localhost:2757/index.html" +
                    "?load=email" +
                    "&userId=" + user.Id +
                    "&code=" + HttpUtility.UrlEncode(code);

如何在将代码传递给UrlEncode之前取回代码的原始值?

1 个答案:

答案 0 :(得分:3)

您可以使用decodeURIComponent() JavaScript函数。此函数将URL编码的字符串作为参数,并返回其未编码的形式。

在您的情况下,您可以解码整个网址

var search = decodeURIComponent($location.search());

或只是代码部分

authService.confirmEmailCode = decodeURIComponent(search.code);