我仍然是Ionic和AngularJS的新手,我有以下问题:我在controller.js
文件中创建了一个服务,现在需要在加载(app.js
)时使用此服务来检测哪个起始页我将前往。如何将服务从controller.js
导出到其他js文件中,以便它可以在app.js
和controller.js
文件中使用。
谢谢,
EL
答案 0 :(得分:2)
使用angular的依赖注入导入它。
如果你的controller.js看起来像:
angular.module('myModule', [])
.service('myService', ['$scope', 'otherDependency', function($scope, otherDependency){
//some service code
}]);
然后,您要使用该服务的模块需要导入服务所在的模块,然后注入服务本身。所以有些东西:
angular.module('app', ['myModule'])
.controller('appCtrl', ['$scope', 'myService', function($scope, myService){
//some code using your service
}]);
此文档可能有所帮助: AngularJS服务 - https://docs.angularjs.org/guide/services AngularJS依赖注入 - https://docs.angularjs.org/guide/di