如何从angularjs应用程序获得服务

how to get a service from angularjs app

本文关键字:服务 应用程序 angularjs      更新时间:2023-09-26

我有一个变量,其中包含我的角度应用

(已用:实例化)

var app = angular.module('app', [...]);

我想获得$timeout服务。

我怎样才能从它那里得到这项服务?

我想要一些类似的东西:

var timeout = app.getService('$timeout');

app.something('$imeout', function($timeout) {
    ...
} // like controller() does

我想在哪里使用它:

define([], function () { // I can import my angular module 'app', or 'angular'
    return {
        'some_function': function () {
            $timeout(function() { ... do something ... }, 1000);
        }
    }
}

这是一项服务(带有requirejs),我的控制器将需要它。

您应该做的是:

app.controller("myCtrl",["$timeout", "otherService" ,function($timeout, otherService){
  $timeout(function() {
        otherService.updateService('Hi');
    }, 3000);
}]);