Calling a function in another function using AngularJS factory

I have an AngularJS factory that has several features.

I want to call one of the functions inside another function like 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 am getting the following error:

AppStart is undefined.

So how can I call a function init()

in a function create_table()

? I tried to just call init()

, but it doesn't work either.

+3


source to share


1 answer


To do this, I recommend defining your named functions 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
     };
});

      

+3


source







All Articles