Angular Js如何在响应结果中编码和解码特殊字符?

时间:2016-10-17 11:10:59

标签: angularjs

我正在努力保存数据,用户也可以输入具有特殊字符的数据。我在保存数据时编码了名称字符串,但作为响应,json数组即将到来。我们如何在获得响应时再次解码数据。

使用编码字符串保存数据。

$scope.saveData = function(){

        $scope.loading = true;
        $scope.Name = encodeURI(this.group.name); 
        $scope.Name1 = encodeURIComponent($scope.groupName);
        $scope.groupData = 'Name='+$scope.Name1+'&Users='+$scope.UserID.join();
        DataService.createUsers($scope.groupData).success(function (data) {

        }).catch(function (err) { 

        })
        .finally(function () {

        });
    }

Js获取控制器中用户的数据。

localStorageService.set('users', grps.allUsers);
localStorageService.bind($scope, 'users');  

function getMyUsers(){
    DataService.getMyUsers().then(function successCallback(response) 
    {

        $scope.users.length = 0; 
        $scope.users.push({'data': response.data.results});

    },function errorCallback(response) {

        console.log(response);

    });  
}

<ul>
 <li ng-repeat="user in users[0].data track by $index"> 
<div class="list_left">
    <div class="pro_text">  
        <h2 class="group_title"><a href="javascript:;">{{ usr.Name }}</a></h2> 

        <p class=""></p>
        <span>Created By: {{ user.createdBy.ownerName }} {{ user.createdBy.lastName }}</span> 
    </div>
</div>
</li>
</ul>   

1 个答案:

答案 0 :(得分:0)

使用decodeURIComponent

var response = {};
DataService.createUsers($scope.groupData).success(function (data) {
     data.yourArray.forEach(function(key, value){
         response[decodeURIComponent(key[0])] = decodeURIComponent(key[1]);
     });
   console.log("new response", response);
})