2016-05-13 43 views
1
//the controller that creates the datatable 
app.controller('AdminListCtrl', function ($scope, $compile, DTOptionsBuilder, DTColumnBuilder, adminService) { 

    var vm = this; 

    function stateChange(iColumn, bVisible) { 
     console.log('The column', iColumn, ' has changed its status to', bVisible); 
    } 

    //vm.dtOptions = DTOptionsBuilder.fromSource('http://localhost/api-v1/admin') 
    vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() { 
       return adminService.loadAdmin(); 
      }) 
     .withPaginationType('full_numbers') 
     .withOption('createdRow', createdRow) 
     // Add Bootstrap compatibility 
     .withBootstrap() 
     // Active ColVis plugin 
     .withColVis() 
     // Add a state change function 
     .withColVisStateChange(stateChange) 
     // Exclude the last column from the list 
     .withColVisOption('aiExclude', [2]) 
     // Add Table tools compatibility 
     .withTableTools('scripts/vendor/datatables/TableTools/swf/copy_csv_xls_pdf.swf') 
     .withTableToolsButtons([ 
     'copy', 
     'print', { 
      'sExtends': 'collection', 
      'sButtonText': 'Save', 
      'aButtons': ['csv', 'xls', 'pdf'] 
     } 
     ]); 

//adminService to request for all administrators 
app.factory('adminService', ['ApiService', function (ApiService) { 
    return { 
     loadAdmin: function() { 
      ApiService.get("admin").then(function (response) { 
       if (response) { 
        if (response.success === true) { 
         return response; 
        }else{ 
         console.log(response); 
        } 
       }else { 
        console.log('error request '); 
       } 
      }); 
     } 
    }; 
}]); 
//apiservice to interact with api 
app.factory('ApiService', function ($http, $q, $localStorage) { 
    return { 
     get: function (apiresource) { 
      var returnData = $q.defer(); 
      $http({ 
       url: api + apiresource, 
       method: 'GET', 
       headers: {"Auth-Token": $localStorage.user_data.auth_token} 
      }) 
       .success(function (data) { 
        returnData.resolve(data); 
       }) 
       .error(function (error) { 
        returnData.resolve(); 
       }); 
      return returnData.promise; 
     }}; 
});`enter code here` 

當我在該視圖中時,它會拋出此錯誤Cananot讀取未定義的屬性'then'。我下面兩種來源 http://www.revillweb.com/angularjs-by-example/4-sharing-data-with-angularjs-services/ http://l-lin.github.io/angular-datatables/#/withPromise獲取當在角度數據表中使用.fromFnPromise時,無法讀取未定義的屬性'then'

回答

2

你需要從loadAdmin方法返回承諾對象(ApiService.get("admin")調用的結果)的例子。

此外,請確保您不要在then(在console.log分支中)「吞下」拒絕 - 當您無意中通過不進一步傳遞錯誤來處理錯誤時會發生什麼情況。對於這種退貨拒絕的承諾或簡單地拋出錯誤,以便拒絕將沿承諾鏈進一步傳播:

app.factory('adminService', ['ApiService', function (ApiService) { 
    return { 
     loadAdmin: function() { 
      return ApiService.get("admin").then(function (response) { 
       if (response) { 
        if (response.success === true) { 
         return response; 
        } else{ 
         console.log(response); 
         throw response; 
         // or custom error object: throw {message: 'Error loadAdmin', response} 
        } 
       } else { 
        console.log('error request '); 
        throw new Error('error request'); 
       } 
      }); 
     } 
    }; 
}]); 
相關問題