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

Ui-grid Async call undefined gridApi in AngularJS

您好,我对 ui-grid 有疑问。当我在 Api 上执行同步调用时,代码会通过 $scope.gridOptions 中的 onRegisterApi

但是如果我将调用放在异步中,它不会传入 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());
            },

But If I put the call in Async, It don't pass in the onRegisterApi and $scope.gridApi is undefined so I loose my filters.

发生这种情况是因为您使用 3d 派对 Ajax 呼叫而不是 angular 方式 $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) {

 });

我找到了解决方案,我必须在 ajax 调用中声明网格选项