1

I need to set a factory variable in my controller. This variable is used for the url for returning a save function in my factory. I actually build this url as several pieces but this is a simplified version.

myApp.factory('SaveDate', ['$resource',
function saveDateFactory($resource, $http) {
  var myData = '';    
  return $resource(myData, {}, { 
    query: { method: "POST", params: {}, isArray: false },
  });                                                                               
}]);    

The function from my controller looks like this:

vm.myFunction1 = function ($resource) {

  vm.myDate = '/test/MyProduct/SetSalesDate/988093099/108/2016/05/21';

  SaveDate.myData = vm.myDate;

  //this should save the date for milestones  
  SaveDate.query();
  vm.cleanUp();

}; 

I have tried using an object instead of a primitive but it also didn't work. I have tested and my value for SaveDate.myData is being set accurately. If I hard code a value in the spot where I declare my variable in the factory, it will save perfectly. I am just not able to pass the variable successfully from my controller to my factory. I have tried a wide variety of ways to do this including using $watch. Thanks for your help on this!

When I added a function like this:

myApp.factory('SaveDate', ['$resource',
function saveDateFactory($resource, $http) {
  var myData = {};
  myData.getMyUrl = function (urlStuff) {
    alert("in SaveDate" + urlStuff);
    return $http.get(urlStuff);
  }

  return $resource(myData.getMyUrl, {}, {  
    query: { method: "POST", params: {}, isArray: false }

Adding this function throws the following error: Unknown provider: myDataProvider <- myData <- UserListController

1
  • 1
    Make a function in your factory that takes the date param and call the function in your controller Commented May 16, 2016 at 22:20

1 Answer 1

4

You'll need to create a function in your factory as Clint said in the comments.

myApp.factory('SaveDate', ['$resource',
function saveDateFactory($resource, $http) {
  var myData = '';

  function setMyData(data) {
    myData = data;
  }

  return {
    setMyData: setMyData,
    resource: $resource(myData, {}, { 
      query: { method: "POST", params: {}, isArray: false },
    })
  };                                                                           
}]);

In your controller:

SaveDate.setMyData(vm.myDate);
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting this error since adding this- SaveDate.query is not a function. Any ideas on this?
It's SaveData.resource now.
Calling this as SaveDate.resource does not throw an error but it also fails to save the data.
How do you call it? If it was SaveDate.query() before then it should be SaveDate.resource().query() now.
I can't thank you enough for your help on this!! Sometimes we get tripped up on what should be an easy little process.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.