Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple functions in my AngularJS Factory?

I'm following the Tutorial from the official AngularJS docs and I want to know if I can add another function to the Phone factory so that I can organize code better. They have declared a "query" function, but what if I wanted to add a query2 function that references a different url...say phones2/:phoneName.json for example?

Factory declaration:

var phonecatServices = angular.module('phonecatServices', ['ngResource']);

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

I have tried a number of things and non of them seem to be working :s

This answer seems to be on the right track, but the syntax for each factory function doesn't quite match up with the above factory.

Something along the lines of:

phonecatServices.factory('Phone', ['$resource',
      function($resource){
       return {
        query: ...
        query2: ...
       }
      }]);
like image 835
SomethingOn Avatar asked Feb 17 '14 05:02

SomethingOn


People also ask

What is the use of factory in AngularJS?

AngularJS Factory Method makes the development process of AngularJS application more robust. A factory is a simple function which allows us to add some logic to a created object and return the created object. The factory is also used to create/return a function in the form of reusable code which can be used anywhere within the application.

What are services in AngularJS?

Services are essentially ways we can share code across our AngularJS applications. Say for instance you have an application that interacts with a RESTful API, you would typically create a factory which would return an object that contains all the functions necessary to interact with that API.

What is the difference between a service and a controller in angular?

Because of this, everytime you switch a route or reload a page, Angular cleans up the currentcontroller. Services however provide a means for keeping data around for thelifetime of an application while they also can be used across differentcontrollers in a consistent manner.

What is the use of calculator service in angular?

CalculatorService – A simple custom angular service that has 2 methods: square and cube. This service has dependency on MathService and it uses MathService.multiply method to do its work. CalculatorController – This is a simple controller that handler user interactions.


2 Answers

One such example of this is: Link for Demo

angular.module('services', []).factory('factoryName', ["$filter",
  function($filter) {
    var method1Logic = function(args) {
      //code
    };
    var method2Logic = function(args) {
     //code
    };
    return {
      method1: method1Logic,
      method2: method1Logic
    };
  }
]).controller('MainController', ["$scope", "$rootScope", "$filter", "factoryName", function ($scope, $rootScope, $filter,factoryName) {
     $scope.testMethod1 = function(arg){
       $scope.val1 = factoryName.method1(arg);
     };

     $scope.testMethod2 = function(arg){
       $scope.val2 = factoryName.method2(arg);
     };
}]);

There is even a better version Opinionated version of this: References

function AnotherService () {

  var AnotherService = {};

  AnotherService.someValue = '';

  AnotherService.someMethod = function () {

  };

  return AnotherService;
}
angular
  .module('app')
  .factory('AnotherService', AnotherService);
like image 120
Abhijeet Avatar answered Sep 18 '22 12:09

Abhijeet


This is the service code:

myServices.factory('Auth', ['$resource',
  function($resource){
    return {
      Login: $resource(serviceURL + 'login', {}, { go: { method:'POST', isArray: false }}),
      Logout: $resource(serviceURL + 'logout', {}, { go: { method:'POST', isArray: false }}),
      Register: $resource(serviceURL + 'register', {}, { go: { method:'POST', isArray: false }}),
    };
  }
]);

And from my controller I just have to add the go() function call to make it work:

Auth.Login.go({ username: $scope.username, password: $scope.password },

I guess I could have named the go function after the method and called it "post()" instead for clarity...

like image 43
SomethingOn Avatar answered Sep 18 '22 12:09

SomethingOn