javascript - Angular ng-grid 和 JSON

标签 javascript angularjs

我对 Angular 还很陌生,我正在尝试在 ui-grid 表中显示我的 JSON。 无论如何,因为这与 ng-grid 类似,我希望有人可以帮助我。

我有一个像这样的 JSON:

{
    Users: [
        username:'blabla',
        email:'bla@gmail.com',
        type:'admin',
        userId: [
            id:'1222'
        ]
    ],
    Teams: [
        teamName: 'team1',
        teamId: '123'
    ]
}

我想要的是:

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

但我无法访问所有对象。仅当我将数据设置为 myDataSource.Users 时,我才能访问用户名

最佳答案

这是一个工作示例:http://jsfiddle.net/8jgpLn9h/

Controller :

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

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

  $scope.data = {
    Users : [{
            username : 'titi',
            email : 'bla@gmail.com',
            type : 'admin',
            userId : {
                id : '1222'
            }
        },{
            username : 'toto',
            email : 'toto@gmail.com',
            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> 

关于javascript - Angular ng-grid 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34402160/

相关文章:

angularjs - Protractor + chrome 驱动程序 : Element is not clickable at point

javascript - D3JS 的 AngularJS 指令是否存在?

javascript - AngularJS 在工厂中创建元素

javascript - AngularJs:-如何在 AngularJs 中将图片动态推送到 div 并上传到服务器

php - 使用 php 和 mysql 获取随机 id

javascript - 使用 webworkers 和 canvas 合并 png 图像

javascript - Serverless 无法导入模块 'handler' 错误

javascript - 如何使用 $window.open() 打开部分 View ?

javascript - JS : Handle with find() property undefined

javascript - 使用 AJAX 提交表单并将数据以 JSON 形式发送到文件