Monday, May 20, 2024
187
rated 0 times [  192] [ 5]  / answers: 1 / hits: 15687  / 12 Years ago, thu, november 15, 2012, 12:00:00

My app.js contains



var app = angular.module('myApp', []).
config(['$routeProvider', function ($routeProvider, $http) {
...
}]);


Service looks like



app.service('MyService', function () {
addNums = function (text) {
return text + 123;
}
});


And in contoller I have



function adminCtrl ($scope, MyService) {
$scope.txt = MyService.addNums(abc);
};


They are all in separate files. The problem is that I'm getting an error
Unknown provider: MyServiceProvider <- MyService



Looks like I'm doing something wrong.


More From » model-view-controller

 Answers
6

The provider error can occur if you forgot to tell Angular to load your myApp module. E.g., do you have this in your index.html file?:



<html ng-app=myApp>


Your service is missing this.:



this.addNums = function(text) {


Fiddle.






There seems to be a lot of confusion in the Angular community about when to use service() vs factory(), and how to properly code them. So, here's my brief tutorial:



The service() method expects a JavaScript constructor function. Many Angular code examples that use service() contain code that is not a constructor function. Often, they return an object, which kind of defeats the purpose of using service() — more about that below. If an object needs to be created and returned, then factory() can be used instead. Often, a constructor function is all that is needed, and service() can be used.



The quotes below are from different AngularJS Google Group posts:




The main difference between using factory() vs service() is that factory()
must return an object, while service() doesn't return anything but it
must be an object constructor function.



Use factory() if the function you are providing builds the object you
want. I.e., Angular will essentially do

obj = myFactory()

to get the obj. Use service() if the function you are providing is
a constructor for the object you want. I.e., Angular will essentially do

obj = new myService()

to get/instantiate the obj.




So when people use service() and its code returns an object, it is kind of a waste because of the way JavaScript new works: new will first create a brand new JavaScript object (then do stuff with prototype, then call the function defined by myService(), etc. -- details we don't really care about here), but because the function defined by myService() returns its own object, new does something a bid odd: it throws away the object is just spent time creating and returns the object that the myService() function created, hence the waste.




One of the reasons that service() was introduced was to make it easy
to use classical OO techniques, such as defining your service as a
coffeescript class.




Also, the undocumented naming convention for services seems to be camelCase with first letter lowercased: e.g., myService.


[#81979] Tuesday, November 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ciarajourneyv

Total Points: 428
Total Questions: 95
Total Answers: 90

Location: Maldives
Member since Sat, Feb 11, 2023
1 Year ago
;