angularjs - How to call an angular resource from a service -


i using angular 1.1.5 (wait angular-ui-router comptabible 1.2.0). defined resource , service:

myapp.factory( 'monitoring', function($resource) {    return $resource('/webapp/network/v1/cronjobs/:id/:action', { id: '@id' }, {        status: { method: 'patch', headers:{'content-type': 'application/json'}, params:{action: 'status'}}    }  ); });  myapp.factory('monitoringcrudcontrollerservice', ['monitoring', '$state', function(monitoring, $state) {     return {         query: function() {             var m = new monitoring();             var promise = m.$query().then(function(response) {                 console.log(response);                 return response.data;             });             return promise;         },         query1: function() {             var m = new monitoring();             return m.$query();         },         // angular 1.2.0 rc         query2: function() {             var m = new monitoring();             return m.$query().$promise;         }            } }]); 

in controller tried query , query1 both of them returned cannot call method 'then' of undefined. query2 should 1.2.0 way call resource.

edit: updated controller

function($scope, $location, monitoringcrudcontrollerservice) {     $scope.refresh = function() {         monitoringcrudcontrollerservice.query().then(function(value) {             $scope.mydata = value;         });     }; } 

console.log(monitoringcrudcontrollerservice) returns object {create: function, get: function, query1: function, query2: function, update: function…}

edit2:

i tried suggested

query3: function() {     return monitoring.query(); }, query4: function() {     var m = new monitoring();     var promise = m.query().then(function(response) {         console.log(response);         return response.data;     });     return promise; }, 

query3 returns promise on can call $then , not then , works.

query4 returns error object #<resource> has no method 'query'. reason used new passing arguments (this how it's documented): see angular resource rest has no method '$save'

inject service in controller like;

yourapp.controller('controllername',function(monitoringcrudcontrollerservice){     $scope.refresh = function() {         monitoringcrudcontrollerservice.query().then(function(value) {             $scope.mydata = value;         });     }; }); 

hope helps.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -