我有一个包含我的 Angular 应用程序的变量
(实例化为:)
var app = angular.module('app', [...]);
我想获得 $timeout
服务。
我怎样才能从it获得这项服务?
我想要这样的东西:
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),我的 Controller 将需要它。
最佳答案
你应该做的是:
app.controller("myCtrl",["$timeout", "otherService" ,function($timeout, otherService){
$timeout(function() {
otherService.updateService('Hi');
}, 3000);
}]);
关于javascript - 如何从 angularjs 应用程序获取服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25983345/