AngularJs: los controladores llaman al método de servicio

I tried to create a method in the services.js :

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

esServices.factory('boxItems', ['$http', function($http) {
              ................
          }]); 

esServices.factory('cartItems', ['$cookieStore', function($cookieStore) {
        array = $cookieStore.get('key');
        var cartItems = new function(){},           
        cartItems.addItem = function(itemSelected){     
        $cookieStore.put('key', []);        
    array.push(itemSelected);
   $cookieStore.put('key', array);   
                }
      }]);

in my controllers I call the service method:

         esControllers.controller('esList', ['$scope','cartItems','$cookieStore',
                    function($scope,cartItems,$cookieStore) {          
              cartItems.addItem($scope.element,function(){});
     };
  }]);

(itemSelected is an object) 

Do you Know if it is possible to pass values (objects) from Controller to Service Method in this way?

Somebody can help me!!!

preguntado el 08 de enero de 14 a las 09:01

Can you post rest of the services.js? -

You don't have a return statement in your service. -

2 Respuestas

esServices.factory('cartItems', ['$cookieStore', function($cookieStore) {
        return {
            addItem: function(itemSelected){
                var array = $cookieStore.get('key');       
                array.push(itemSelected);
                $cookieStore.put('key', array);
            },
            removeItem: function(){
                //...
            }
        }    
}]);

then call using

cartItems.addItem(itemSelected);

Respondido el 08 de enero de 14 a las 11:01

But I have the method addItem, removeItem and getItem - user880386

so then you add another function called removeItem in the same way, addItem:function(){...}, removeItem: function(){...}. you then call each of these methods in the controller using cartItems.method(). Angular services need a return statement and in this instance you're returning an object of functions - cejast

Before var array = $cookieStore.get('key'); I tried to print itemSelected, this object is empty....maybe I wrong to obtain the object from controller... - user880386

Do you Know if it is possible to pass values (objects) from Controller to Service Method in this way? - user880386

you just pass it through the function parameters like you would any other function. so if you wanted to pass $scope.object from the controller to your addItem() function in the service you would call cartItems.addItem($scope.object). - cejast

You should inject the service in the controller like

        var app = angular.module('app', ['ngCookies'] );
        app.factory('cartItems', ['$cookieStore', function($cookieStore) {
          return {
             addItems : function(){
                alert('hello');   
             }  
          }
       }]);
       app.controller('MyController',function($scope,cartItems){
         $scope.test = 'my test';
         cartItems.addItems();
      });

If you want to use your ugly syntax :) just return cartItems from your factory

Respondido el 08 de enero de 14 a las 10:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.