0

This is get service function where I am calling the API

    .factory('Report', function($resource, API_URL) {
       return $resource(API_URL  + 
       'security/:userId/1498780800000/listOfDeliveries', {

      userId : '@userId',
      expected : '@expected',
      arg1 : '@arg1'
    }, {
      update: {
        method: 'PUT'
      }
    });
  })

In the app.js I have this below controller

    .controller('ReportsController', function($scope, $rootScope, 
     ProfileData, $state, $timeout, Report) {


})
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
Bhavani
  • 41
  • 7

1 Answers1

1

First of all, you need to check how angular factory & service work.

Your factory return a $resource, so read the doc about $resource

A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:

{ 
   'get':    {method:'GET'},
   'save':   {method:'POST'},
   'query':  {method:'GET', isArray:true},
   'remove': {method:'DELETE'},
   'delete': {method:'DELETE'}
};

So, you can use theses methods: Report.get(), Report.save(), Report.query(), Report.remove(), Report.delete()

In addition there are custom method you defined: Report.update()

And you can pass userId as params so:

Report.get({userId: 1234}); will call a GET request to: API_URL+'security/1234/1498780800000/listOfDeliveries

(expected and args1 are not in url so I dont think you need them)

What's returning Report.get() ?

Class actions return empty instance (with additional properties below). Instance actions return promise of the action

So Report.get(...) is returning a promise, you will get data by:

Report.get(...).then(function(data) {
  $scope.requestData = data;
});
Fetrarij
  • 7,176
  • 3
  • 27
  • 35
  • Thanks Fetra.. Its working .. may i know how to pass this timestamp parameter dynamically – Bhavani Jul 05 '17 at 05:09
  • yes, you can add a params like userId or expected: return $resource(API_URL + 'security/:userId/:timestamp/listOfDeliveries', { userId : '@userId', timestamp: '@timestamp',.... – Fetrarij Jul 05 '17 at 05:45
  • I have added param timestamp.. I am new to hybrid app development. can you please explain me why this below code is not working. – Bhavani Jul 05 '17 at 06:09
  • $scope.requestData = { expectedDateDate : '1498780800000' }; $scope.residents = Report.get({userId: Safence_App.userId},{timestamp: requestData.expectedDateDate}); – Bhavani Jul 05 '17 at 06:09
  • It's not from hybrid app it's angular. You should really read the the link I give you from $resource, it will return a promise, and there are example from the official site I gave you. I'm editing the answer to help you. – Fetrarij Jul 05 '17 at 06:14