Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8884

Getting not defined on factory

I am trying to use ionic with angular to pass data from one controller to anther, I know that the best way to do it with factory, But I am getting error:

ReferenceError: setData is not defined

my code is

app.factory("Places", function() {
    var Places = {};
    Places.setData = function(places) {
      Places.items = places;
    };
     Places.getItem = function($stateParams) {
      return Places.item;
    };

    return{
        setData: setData,
        getItem:getItem
    }
  })

and the controler

   .controller('DetailsCtrl', function ($scope, $stateParams,Places) {
          console.log('PlaceitemCtrl');
        $scope.items=Places.getItem($stateParams);
    });

Thanks for help!

Upvotes: 0

Views: 840

Answers (1)

dfsq
dfsq

Reputation: 193261

You are returning new object from the factory and at the same time there are no local setData and getItem functions defined. Instead return Places object which has necessary methods attached:

app.factory("Places", function () {
    var Places = {};
    Places.setData = function (places) {
        Places.items = places;
    };
    Places.getItem = function ($stateParams) {
        return Places.item;
    };

    return Places;
});

Upvotes: 4

Related Questions