2015-12-21 46 views
1

我很新的Angular,即時嘗試在ui網格表中顯示我的JSON。 反正,因爲這與ng-grid類似,所以我希望有人能幫助我。角ng網格和JSON

我有這樣一個JSON:

{ 
    Users: [ 
     username:'blabla', 
     email:'[email protected]', 
     type:'admin', 
     userId: [ 
      id:'1222' 
     ] 
    ], 
    Teams: [ 
     teamName: 'team1', 
     teamId: '123' 
    ] 
} 

我要的是:

$scope.gridOptions = { 
data: myDataSource, 
columnDefs: [{ field: 'Users.userName', displayName: 'userName', width: 90 }] 

但是我無法訪問所有對象。只有當我把我的數據myDataSource.Users

+0

你是什麼意思你無法訪問所有的對象?你能給個例子嗎?您無法訪問哪個對象或字段?請明確點。 –

+0

你的JSON無效 –

回答

0

這是一個工作的例子,我可以訪問用戶名:http://jsfiddle.net/8jgpLn9h/

控制器:

var app = angular.module('app', ['ngTouch', 'ui.grid']); 

app.controller('MainCtrl', ['$scope', function ($scope) { 

    $scope.data = { 
    Users : [{ 
      username : 'titi', 
      email : '[email protected]', 
      type : 'admin', 
      userId : { 
       id : '1222' 
      } 
     },{ 
      username : 'toto', 
      email : '[email protected]', 
      type : 'admin', 
      userId : { 
       id : '1' 
      } 
     } 
    ], 
    Teams : { 
     teamName : 'team1', 
     teamId : '123' 
    } 
}; 
$scope.gridOptions = { data: 'data.Users' }; 

}]); 

HTML:

<div ng-app="app"> 
    <div ng-controller="MainCtrl"> 
     <div ui-grid="gridOptions" class="grid"></div> 
    </div> 
</div> 
+0

但如果用戶是一個數組,它不可能使用? – lili

+0

是的,這是可能的。你創建'Users'? – AlainIb

+0

我編輯我的答案 – AlainIb