JSON.parse不会将JSON字符串转换为Angular中的对象

时间:2014-06-26 23:23:53

标签: javascript json angularjs

我为角度应用构建基于角色的登录,当您第一次输入凭据时,会发回一个会话对象,其中包含一系列属性,包括用户可以选择登录的角色数组。当用户选择他们希望登录的相应角色的单选按钮时,例如, "管理员",我将ng-model' to update $ scope.model.selected`用于他们选择的任何对象。

但是,当我需要它作为一个javascript对象时,UI $scope.model中的某些点被解析为一个字符串。简而言之,我试图将我的模型转换回对象而我无法让JSON.parse($scope.model)工作。

这是我的控制人员:

$scope.submit = function() {
      $http
        .post(app.api.root + 'session-tokens', $scope.user)
        .then(
          // SUCCESS
          function (response) {
            $scope.model = { selected: response.data.results.roles[0] };
            $scope.results = response.data.results;
            $scope.isCorrect = true;
            $scope.welcome = 'Welcome, ' + $scope.user.username + '!';

            // redirect after login -- need to change this to a profile page
            // $location.path('/patients/22');
            $scope.login = function() {
              $scope.model = JSON.parse($scope.model);
              authSvc.user({
                id: response.data.results.userId,
                token: $scope.model.selected.token,
                role: $scope.model.selected.roleName,
                providerName: $scope.model.selected.providerName,
                username: $scope.user.username,
                isAuthenticated: true
              });
              $scope.user = authSvc.user();
              console.log(typeof $scope.model.selected);
            };
          }
};

我想要进行转换的功能是$scope.login(),在用户选择角色并点击登录后触发。

知道如何才能让它发挥作用吗?而且我不认为我使用angular.fromJson这个新的角度版本。

感谢。

修改

这是我在控制台中遇到的错误:

SyntaxError: Unexpected token o
at Object.parse (native)
at Scope.$scope.login (eval at <anonymous> (http://localhost:4000/$js/jquery.js:336:5), <anonymous>:32:39)
at http://localhost:4000/$js/angular.js:10288:21
at http://localhost:4000/$js/angular.js:18086:17
at Scope.$eval (http://localhost:4000/$js/angular.js:12045:28)
at Scope.$apply (http://localhost:4000/$js/angular.js:12145:23)
at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
at HTMLFormElement.<anonymous> (http://localhost:4000/$js/angular.js:18085:21)
at HTMLFormElement.jQuery.event.dispatch (http://localhost:4000/$js/jquery.js:4371:9)
at HTMLFormElement.elemData.handle (http://localhost:4000/$js/jquery.js:4057:28) 

修改

所以$scope.model实际上返回一个对象,但是$scope.model.selected返回一个json字符串,我需要它作为一个对象。

返回的字符串是:

{"roleName":"Group Provider","providerId":100,"providerName":"TestProvider","token":"XXX"}

CONSOLE.LOGS

console.log(response.data.results):
Object {userId: 3, roles: Array[2]}

console.log(response.data.results.roles[0]):
Object {roleName: "Administrator", providerId: 2, providerName: "TestLastNAme, Test", token: "xxx", $$hashKey: "0fL"}

console.log($scope.model):
Object {selected: "{"roleName":"Group Provider","providerId…Nn0.lwB6AggcMkvH_LcQzpUdLlbk3XBHTGBqCd8K07HBIfo"}"}

console.log($scope.model.selected):
{"roleName":"Group Provider","providerId":237,"providerName":"TestProvider","token":"xxx"} 

console.dir($scope.model):
Object
    selected: "{"roleName":"Insurance Group Provider","providerId":237,"providerName":"TestProvider","token":"xxx"}"
    __proto__: Object

2 个答案:

答案 0 :(得分:1)

当您尝试在对象上使用JSON.parse()时,会发生该错误。

答案 1 :(得分:1)

所有这一切都证明问题在于我在单选按钮上使用value="{{model}}"而不是角度ng-value="model"value=会将对象解析为字符串,而ng-value会将其保留为对象。