0

I want to send api call controller.js to service.js in my demo.i am show user list in demo and i am used for angularjs datatable with web api.and my structure is app.js,controller.js,service.js this 3 separate file is used. then i want to send call service.js.

this is my simple controller.js code working well.

app.controller('userscontroller', ['$scope', '$http', 'DTOptionsBuilder', 'DTColumnBuilder', 'userservice',
function ($scope, $http, DTOptionsBuilder, DTColumnBuilder, userservice) {

    $scope.dtColumns = [
        DTColumnBuilder.newColumn("fullName", "Full Name").withOption('name', 'firstname'),            
        DTColumnBuilder.newColumn("email", "Email").withOption('name', 'email'),            
    ]

    $scope.dtoptions = dtoptionsbuilder.newoptions().withoption('ajax', {
        datasrc: "data",
        url: "/home/getuserlist",            
        type: "post",
        data: { 'type': "time"},            
    })
    .withoption('processing', true) 
    .withoption('serverside', true) 
    .withpaginationtype('full_numbers') 
    .withdisplaylength(10)
    .withoption('aasorting', [0, 'asc'])       

}])

this my service.js file:

app.service('userservice', function ($http) {});

any one have idea how to call this api call using service.js then please let me know.

coderwill
  • 804
  • 3
  • 16
  • 38

1 Answers1

0

Based on your comments, you can do it like this:

Your user service:

app.service('userservice', ['$http', '$q', function ($http, $q) {

  function getuserlist(data){
    var defer = $q.defer();
    $http.post('/home/getuserlist', data)
    .then(function(result){
       defer.resolve(result.data);
    }); 
    return defer.promise;
  }

  return {
    getUsers: getuserlist
  };
}]);

Then, in your controller, you can utilize fromFnPromise function, something like this:

vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() {
        return userservice.getUsers(data);
    })
    //rest of your options

You can find more details about fromFnPromise method on this link.

Emin Laletovic
  • 4,084
  • 1
  • 13
  • 22
  • how can manage "$scope.dtoptions = dtoptionsbuilder.newoptions().withoption(" this code in your code please let me know. actully this for used get list of user. – coderwill Mar 09 '17 at 07:31
  • So, you want to use `dtoptionsbuilder` instead of plain `$http`? – Emin Laletovic Mar 09 '17 at 07:36
  • may be yes i don't how to used this api call are you explain in details so it's batter for me. – coderwill Mar 09 '17 at 07:45
  • Ok, couple more questions, just to get a clear picture here. Your current code is working and getting the data from the api, but you want to move that api call to a service but still make it using the `DTOptionsBuilder`? Or it's not working at all? – Emin Laletovic Mar 09 '17 at 08:28
  • yes my current code is working and getting data.but i want to api call using service.js?so i am here confuse how to call api and manage all stuff. – coderwill Mar 09 '17 at 08:41
  • There is no need to manufacture a promise with $q.defer as the $http service already returns a promise. In addition, the promise will hang and fail to resolve if the $http promise resolves with a rejection. This is a classic example of an erroneous [Deferred Anti-Pattern](http://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg Mar 09 '17 at 13:31
  • yes, you are correct, $q.defer is not necessary here. thanks! – Emin Laletovic Mar 09 '17 at 13:40