Ui-grid异步调用AngularJS中未定义的gridApi

时间:2017-08-24 09:48:21

标签: angularjs ajax ui-grid

您好我的ui-grid有问题。当我在Api上进行同步通话时,代码将通过onRegisterApi中的$scope.gridOptions

但是,如果我将调用置于异步中,则不会传递onRegisterApi$scope.gridApi未完成,因此我会丢失过滤器。

以下是我对GridOptions的定义:

 $scope.gridOptions = {                     
                    colDef = colDef,
                    data = $scope.rowCollection,
                    enableGridMenu: true,
                    enableFiltering: true,
                    onRegisterApi: function (gridApi) {
                        debugger; //Don't pass here with async call
                        $scope.gridApi = gridApi;

                    }
                }

这是我的api电话的开始:

$.ajax({
            url: Api.getHost() + "url/" + $scope.currentObjectType,
            async: true,
            type: "GET",
            datatype: "jsonp",
            contentType: "application/json",
            data: { page: $scope.page, number: $scope.number },
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + Api.getToken());
            },

2 个答案:

答案 0 :(得分:1)

  

但是如果我把调用放在Async中,它不传入onRegisterApi并且$ scope.gridApi是未定义的,所以我松开了我的过滤器。

之所以发生这种情况,是因为您使用了3d方Ajax调用,而不是角度方式$http

选项1

出于这个原因,尝试使用$timeout,如:

// ...
$timeout(function(){
   $scope.gridOptions.data = YourNewData; 
})

选项2:使用$http

 $http({method: 'GET', url:URL}).then(function (result) {
      $scope.gridOptions.data = result.data;                            
 }, function (result) {

 });

答案 1 :(得分:0)

我找到了解决方案,我不得不从ajax调用中声明网格选项

相关问题