class ControllerA {
static $inject = ['$http', '$scope','app.services.CommonService'];
constructor(
private $http: ng.IHttpService,
private $scope: ng.IScope,
private commonService: app.services.ICommonService){
}
//calling a common method from controller A
this.commonService.CommonMethod();
}
class ControllerB {
static $inject = ['$http', '$scope','app.services.CommonService'];
constructor(
private $http: ng.IHttpService,
private $scope: ng.IScope,
private commonService: app.services.ICommonService){
}
//executing a common method from controller B
this.commonService.CommonMethod();
}
module app.services {
//common interface for making service accessible
export interface ICommonService {
CommonMethod();
}
export class CommonService implements ICommonService{
static $inject = ['$http'];
constructor(private $http: ng.IHttpService) {
}
public CommonMethod(){
//implementation of a common method here
}
}
factory.$inject = ['$http'];
function factory($http: ng.IHttpService) {
return new CommonService($http);
}
angular.module('yourApp')
.factory('app.services.CommonService', factory);
}
Using a shared service to expose commonly used methods was suggested by Kobi.