3

I have an AngularJS factory that has multiple functions.

I want to call one of the functions inside the other function as shown below:

.factory("AppStart", function($cordovaSQLite) {
  return {
    init: function() {
      var res = "hello";
      console.log("in load start up page");  
    },
    create_table: function() { 
      AppStart.init();
    }
  }
});

But I get the following error:

AppStart is not defined.

So how do I call the init() function in the create_table() function? I have tried just calling init(), but it doesn't work either.

2
  • Remove AppStart.init() from inside the create_table function, inject AppStart in your controller and call AppStart.init from there Commented Apr 27, 2015 at 15:19
  • well i dont hv a controller Commented Apr 27, 2015 at 17:03

1 Answer 1

3

To accomplish this, I recommend defining your functions with names, and then creating a service object with properties that refer to them, as I did below:

.factory("AppStart", function($cordovaSQLite) {
    function init() {
        var res = "hello";
        console.log("in load start up page");  
    }

    function create_table() {
        init();
    }

    return {
        init: init,
        create_table: create_table
     };
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.