raju
raju

Reputation: 5008

How does angularjs services work

I am building my first real world Angularjs application. Object Menus is created in services by following code of my boiler plate code

angular.module('mean.system').factory('Menus', ['$resource', function($resource) {
    return $resource('admin/menu/:name', {
        name: '@name',
        defaultMenu: '@defaultMenu'
    });
}]);

How does this work as in all the turorials I have seen http calls are made by explicitly mentioning the url format and type of request like in following

phonecatServices.factory('Phone', ['$resource',
  function($resource){
    return $resource('phones/:phoneId.json', {}, {
      query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
    });
  }]);

Upvotes: 0

Views: 231

Answers (2)

ed.
ed.

Reputation: 2706

I think it would be fair to say that this is a question about how $resource works? If that is the case the best place to start is having a look at the docs and the source.

Againt this version of the source, when you invoke $resource(....) you are calling the internal resourceFactory which is building the resource out of the parameters you have provided and some sensible defaults.

Upvotes: 1

Ruslan  Ismagilov
Ruslan Ismagilov

Reputation: 1348

For better explanation you could check Angular.js $resource docs
Menus factory works practically the same as Phonefactory. It is return $resource object by which you can call a query with name and defaultMenu params. For example:

// set menu and default params
var name = 'admin'
  , default = 'main';

Menus.query({
  name: name,
  defaultMenu: default
}, function(menu) {
   // process responce from the server
   // what it should return?
   $scope.menu = menu;
});

Upvotes: 1

Related Questions