角度数据表使用源对象

时间:2015-10-29 11:03:19

标签: ajax angularjs angular-datatables

使用Angular Datatables我想用Ajax预加载一个JSON对象,这样我就可以在其他地方重用该对象,而无需再做一个ajax请求。 但是如何将此对象加载到数据表中?

    .controller('ResponsiveDatatableCtrl', function ($scope, $rootScope, DTOptionsBuilder, DTColumnBuilder, apiserv, $filter, $state, $http) {


    $scope.dataLoading2 = true;
    var vm = this;  
    var data = "?db="+ $rootScope.globals.currentUser.agents[$rootScope.globals.currentDB].db_name;
    var url = apiserv+"api.files.php"+data;

    var headers = {'Content-Type': 'application/x-www-form-urlencoded'};
    $http({
        method: 'POST',
        url: url,
        headers: headers,

    })
        .success(function (response) {
            $rootScope.globals.files = response;
            $scope.dataLoading2 = false;
            //console.log($rootScope.globals.files);


        });

    vm.dtOptions = DTOptionsBuilder.fromFnPromise($rootScope.globals.files)
        .withPaginationType('full_numbers')
        .withBootstrap() 
        .withOption('responsive', true);

})

1 个答案:

答案 0 :(得分:1)

好的我已经尝试了以下内容,它似乎成功调用了我的代码但是表格没有更新?

    vm.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
        url: url,
        type: 'POST',
        headers: headers,
        data: function(data, dtInstance) {

        },
        success: function(response) {
            $rootScope.globals.files = response;
        }
    })
        .withPaginationType('full_numbers')
        .withBootstrap()
        .withOption('responsive', true);