20

Angular Grid を使用して、console.log で ajax get データを取得します。しかし、空のグリッド。

コンソール ログには次のように表示されます。

[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>
4

2 に答える 2

26

リクエストに対してサービスを定義した場合、これははるかに簡単になります (そしてより Angular になります)。これらの行に沿ったもの:

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

アプリに必ず含めてください。

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

次に、コントローラーでそれに対する promise を取得できます。

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

次に、グリッド オプションを設定して、そのデータの約束を確認します (既に行ったように)。

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

これを行うと、 $scope.$apply は処理されるため、心配する必要はありません。これは、はるかにクリーンで従うのが簡単です。サーバーから返されたデータを変更するためにコールバックが必要な場合は、query()関数をサービスの関数に渡します。

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

Angular Servicesの公式ドキュメントを確認してください。基本は、公式の Angular JS チュートリアルのステップ #11でも説明されています。

于 2013-03-06T18:13:38.090 に答える
13

コントローラーは、.success が完了する前に getData 配列にアクセスしている可能性があります。空の配列に初期化されている promise 関数の外部で、すぐに変数にアクセスしています。

(今のところ) fetchData 関数をコントローラーに入れ、getData を .success の $scope.myData に直接格納してみませんか? たぶん、グリッドも初期化しますか?それができるかどうかはわかりませんが、できる場合は次のようになります。

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();    
});

(一部の $scope 適用のソース: https://github.com/angular-ui/ng-grid/issues/39 )

また、jQuery .ajax を angular コードと混合している理由 ($http がそれを行います) と、javascript にセミコロンがない理由もわかりません。

于 2013-02-27T19:50:48.093 に答える