Get Ajax data into Angular grid

后端 未结 2 1257
梦如初夏
梦如初夏 2021-02-05 07:19

Using Angular Grid, I get the ajax get data in console.log. But an empty grid.

The console log reads:

[13:56:11.411] now!!
[13:56:11.412] []
[13:56:11.4         


        
2条回答
  •  青春惊慌失措
    2021-02-05 07:42

    Your controller is probably accessing the getData array before the .success is finished. You're accessing the variable right away, outside of a promise function, which is initialized to an empty array.

    Why don't you try putting the fetchData function into the controller (for now) and storing the getData directly into $scope.myData in the .success? Maybe even initialize the grid right there too? Not sure if you can do that but if you could it would look like this:

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

    (source for some of the $scope apply stuff: https://github.com/angular-ui/ng-grid/issues/39)

    Also not sure why you're mixing in jQuery .ajax with angular code ($http will do that), and why none of your javascript has a semicolon.

提交回复
热议问题