-->

获取阿贾克斯数据转换成角度网格(Get Ajax data into Angular grid)

2019-08-17 05:23发布

采用了棱角分明格,我得到了阿贾克斯的console.log获取数据。 但是一个空网格。

控制台日志上写着:

[13:56:11.411] now!!
[13:56:11.412] []
[13:56:11.412] now!!
[13:56:11.556]  <there is data returned from console.log(getData); > 

这是js文件。

// main.js
var app = angular.module('myApp', ['ngGrid']);

var getData = [];

function fetchData() {
    var mydata = [];

    $.ajax({
        url:'/url/to/hell',
        type:'GET',
        success: function(data) {

            for(i = 0, j = data.length; i < j; i++) {
                mydata[i] = data[i];
            }
            getData = mydata;
            console.log(getData);
        }
    });    
 }
fetchData();     


app.controller('MyCtrl', function($scope) {    

    console.log('now!!')
    console.log(getData)
    console.log('now!!')

    $scope.myData = getData


    $scope.gridOptions = { 
        data: 'myData',
        showGroupPanel: true
    };
});

新的js文件:

// main.js

var app = angular.module('myApp', ['ngGrid']);

app.controller('MyCtrl', function($scope, $http) {          
function fetchData() {
    $http({
        url:'/url/to/hell',
        type:'GET'})
        .success(function(data) {
            $scope.myData = data;       
            $scope.gridOptions = { 
                    data: 'myData',
                    showGroupPanel: true
                };              
        });         
}   
fetchData();    
});

HTML文件。

<html ng-app="myApp">
        <head lang="en">
            <meta charset="utf-8">
            <title>Blank Title 3</title>  
            <link rel="stylesheet" type="text/css" href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" />
            <link rel="stylesheet" type="text/css" href="../static/css/style.css" />
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
            <script type="text/javascript" src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script>
            <script type="text/javascript" src="../static/js/main.js"></script>
        </head>

        <body ng-controller="MyCtrl">

            <div class="gridStyle" ng-grid="gridOptions"></div>
        </body>
    </html>

Answer 1:

你的控制器可能访问的getData阵列.success完成之前。 你所访问的变量向右走,一个承诺的功能,它被初始化为空数组之外。

你为什么不尝试把fetchData功能到控制器(现在)和存储的getData直接到$ scope.myData在.success? 甚至初始化格在那里呢? 不知道如果你能做到这一点,但如果你能是这样的:

app.controller('MyCtrl', function($scope, $http) {  
$scope.myData = '';
$scope.gridOptions = { showGroupPanel: true, data: 'myData' };
function fetchData() {
    setTimeout(function(){  
        $http({
            url:'/url/to/hell',
            type:'GET'})
            .success(function(data) {
                $scope.myData = data;   
                if (!$scope.$$phase) {
                    $scope.$apply();
                }                   
            });         
    }, 3000);       
}   
fetchData();    
});

(来源对于一些$范围的申请材料: https://github.com/angular-ui/ng-grid/issues/39 )

另外不知道你为什么在jQuery的阿贾克斯与角码混合($ HTTP将做到这一点),为什么没有你的JavaScript中有一个分号。



Answer 2:

如果您的请求中定义的服务,这将是更容易(多角度)。 这些方针的东西:

angular.module('hellServices', ['ngResource'])
  .factory('Hell', function ($resource) {
    return $resource('URL/TO/HELL', {}, {
      query: { method: 'GET' }
    });
  });

确保包括它在你的应用程序:

var app = angular.module('myApp', ['ngGrid', 'hellServices']);

然后,你可以在你的控制器,它承诺:

app.controller('MyCtrl', function($scope, $http, Hell) {  
$scope.myData = Hell.query();

然后设置网格选项来看看它的数据的承诺(因为你已经做了):

$scope.gridOptions = { 
    data: 'myData',
    showGroupPanel: true
};

如果你这样做,你不必担心$范围。$适用,因为它会为你处理。 这是更清洁和更容易理解。 如果您还需要一个回调来修改数据,一旦它从服务器返回,传递一个函数的query()服务的功能:

...
$scope.myData = Hell.query(function(hell) {
    // code that modifies 'hell'
});

退房的官方文档上的角服务。 基础知识,也包括在步骤#11官方角JS教程。



文章来源: Get Ajax data into Angular grid