使用AngularJS工厂在另一个函数中调用函数 [英] Calling a function in another function with AngularJS factory

查看:175
本文介绍了使用AngularJS工厂在另一个函数中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个功能的AngularJS工厂。

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();
    }
  }
});

但我收到以下错误:


未定义AppStart。

AppStart is not defined.

那么如何调用 init( )函数在 create_table()函数中?我试过调用 init(),但它也不起作用。

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.

推荐答案

为了实现这一点,我建议使用名称定义函数,然后创建一个具有引用它们的属性的服务对象,如下所示:

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
     };
});

这篇关于使用AngularJS工厂在另一个函数中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆