2015-10-05 49 views
1

我想顯示一個JSON對象的內容。在客戶端,我使用ng-repeat遍歷對象名稱Users來獲取ID,名稱和密碼。但是,我只在客戶端獲取'[object Object]而不是預期的值。AngularJS:在客戶端顯示JSON對象內容

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

回答

2

你不應該stringify的JSON。

$scope.Users = data.data; 

您應該在顯示輸出時使用string並置。

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

它仍然給我[對象對象]。我做了data.data,因爲實際對象如下所示:{「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「} –

+1

@JeanB ok然後您應該使用'$ scope.Users = data.data'沒有stringify –

+0

我嘗試了同樣的例子在plunker,它的工作原理。我不得不做一些小的改變,比如向templatUrl添加.html並刪除htmlModle(true)。 –