How to share common functionalites across two angular apps

angularjs

Solution

If you make one module for each app and one module for your shared services then

var serviceModule= angular.module('ServiceModule', []);
serviceModule.factory("Resourcesvc", ["$http", "$q", Resourcesvc]);

var purchaseModule= angular.module('PurchaseModule', ['ServiceModule']);
purchaseModule.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl]);

var salesModule= angular.module('SalesModule', ['ServiceModule']);
salesModule.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl]);

Now your service is only declared once, in a `ServiceModule` and you have two other "apps" (really `modules` as you identified in your question) that each declares a dependency on it. `Resourcesvc` can now be injected into any controller/service in both modules.

Declaring dependencies on other modules (both your own and 3rd party ones) are what the `[]` part of a module declaration is for.

Problem

In my one project i have two apps. lets say PurchaseApp and SalesApp. These two apps are using a shared service (or factory) called Resourcesvc.js. Currently the structure of two apps are given below PurchaseApp.js ``` var PurchaseApp= angular.module('PurchaseApp', []); PurchaseApp.factory("Resourcesvc", ["$http", "$q", Resourcesvc]); // Registering Resource DI in app PurchaseApp.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl]); ``` SalesApp.js ``` var SalesApp= angular.module('SalesApp', []); SalesApp.factory("Resourcesvc", ["$http", "$q", Resourcesvc]); / Registering Resource DI in app SalesApp.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl]); ``` Its working fine now . but is there any better approach or practice available to share common service across module /Apps . ( Really i am confusing angular app vs module)

Original source