Because of how DI works, you don’t need to instantiate your services, ever really. What you do is that you enter the services you need into your controller, and it should just work. In the above case, your controller may be defined as:
var app = angular.module('App', ['DataProcessor']); function MyController($scope, DataProcessor) { var uri = ''; DataProcessor.processData(uri); }
The only thing you need to do is make sure that “Application” is the name specified in the “ng-app” directive and make sure your page contains JS files with “DataProcessor” before you enable the angular application module (technically they can be defined in one file). Hope this helps!
Edit
By the way, if you need to minimize - this is how you define the controller:
var app = angular.module('App', ['DataProcessor']); // if you need to minify: var MyController = ['$scope', 'DataProcessor', function($scope, DataProcessor) { var uri = ''; DataProcessor.processData(uri); } ];
Additional offers
My understanding of the service at the moment is that it is used to share data or code between controllers. If this data processing is specific to this controller, you can simply transfer the "ProcessData" implementation directly to your controller. Sometimes such changes may be easier than processing data in a service. If you process data in a service, you can still write that data back to the area. In this case, you can pass $ scope as a parameter to the service procedure. Since I don’t know too much about your use case, just accept these recommendations with salt. Good luck
drew_w
source share