【问题标题】:How to call a function from a seperate file and controller in angular js ? is it possible?如何从 angularjs 中的单独文件和控制器调用函数?可能吗?
【发布时间】:2019-02-14 08:55:15
【问题描述】:

例如,我的 test.js 包含控制器 testCtrl,该控制器具有以下代码的功能。我可以访问和使用/调用吗 那个函数是我的 test2.js 上的 get_data ,它有控制器 test2Ctrl ?

#testCtrl (test.js)  
    me.get_data = function () {
            dataservice.service(me).then(function (response) {
                if (response.status == "200") {
                    me.records.questionaires = response.data


                }
            })
        }

【问题讨论】:

  • 把这个函数放到服务中,那个服务你可以注入到两个控制器中。

标签: javascript angularjs function controller


【解决方案1】:

假设您的文件已正确包含在站点中,您可以将 get_data 移至 angularjs 服务。然后您可以inject the service 进入每个控制器并调用它。

如果控制器的作用域是父子关系,如果父定义了$scope.get_data,子控制器可以使用$scope访问它。您可以在 $rootScope 中定义 get_data 来实现相同的目的,只需确保在任何控制器调用它之前创建它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多