AngularJS:在客户端显示JSON对象内容

时间:2015-10-05 17:09:25

标签: angularjs asp.net-mvc-4 controller client jsonobject

我正在尝试显示JSON对象的内容。在客户端,我使用ng-repeat来遍历对象名称Users以获取ID,名称和密码。但是,我只是在客户端获取[object Object]而不是预期的值。

//The controller that I am using
 myApp.controller('userController', function ($scope,$http /*UserService*/) {
      //  $scope.Users = [];
        $http.get('/Templates/ListUsers')
        .success(function (data) {
            // $scope.Users = data.data;
            if (data.Ok) {
                $scope.Users = JSON.stringify(data.data);
                console.log($scope.Users);
            }
        }).error(function(error){
            console.log(error);
        });

//The form where the data is supposed to display. 

<div class="row">
       <div class="form-group">
           <li ng-repeat="x in Users"> 
               {{ x.ID, x.Name, x.Password  }}
           </li>
       </div>
   </div>   

//The JSON object based on the line console.log($scope.Users);
{"Ok":true,"data":[{"ID":1,"Name":"Name1","Password":"Password1"}, 
{"ID":2,"Name":"Name2","Password":"Password2"}
,{"ID":3,"Name":"Name3","Password":"Password3"},
{"ID":4,"Name":"Name4","Password":"Password4"}],"message"
 :"Success"}

以下是plunker中的相同示例,它可以正常工作。

http://plnkr.co/edit/btL2QMyHxhDLH7cxZ1YV?p=info

1 个答案:

答案 0 :(得分:2)

你不应该是stringify JSON。

$scope.Users = data.data;

显示输出时应使用string连接。

<li ng-repeat="x in Users"> 
     {{ x.ID +','+ x.Name +','+ x.Password  }}
</li>