[英]Calling a function in another function with AngularJS factory
I have an AngularJS factory that has multiple functions. 我有一个具有多种功能的AngularJS工厂。
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.
AppStart未定义。
So how do I call the init()
function in the create_table()
function? 那么如何在
create_table()
函数中调用init()
create_table()
函数呢? I have tried just calling init()
, but it doesn't work either. 我试过调用
init()
,但它也不起作用。
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
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.