1.factory
Factory, you provide a method that returns an instance of an object, for the factory of AngularJS, is to define an object, add properties and methods to the object, and then return the object, for example:
var app = Angular.module (' MyApp ', []); App.factory (' MyFactory ', function () { //Define result object of factory. var result = {}; Add some property and method to the object result.greeting = ' Hello from factory. '; Return the object; return result;});
Finally, the controller gets the result object:
M1.controller (' Aaa ', [' $scope ', ' myfactory ',function($scope, myfactory) { console.log (myfactory); // The myfactory here is the result object }]);
2.service
The service new is instantiated by an operator and can be considered a type, as long as the properties and methods are added to the this object without explicitly returning any objects, such as the following code:
function () { this. Greeting = ' Hello from service ';});
The controller gets the object that is pointed to in the code above this ,
M1.controller (' Aaa ', [' $scope ', ' Myservice,function ($scope, MyService) { console.log (myservice); The MyService here is the object above this point, which is equivalent to var myservice = new MyService ();}]);
3.provider
Slightly different from factory and service, provider must provide a $get method that $get is consistent with the factory requirements: defining an object, adding properties and methods to the object, and returning the object, for example:
function () { thisfunction() { var result = {}; = ' Hello from provider '; return result; }})
The last thing the controller gets is the object $get returned by the provider method,
M1.provider (' Myrandomnum ',function(){ return{bolint:false, int:function(argbol) {if(argbol) { This. Bolint =true; } Else{ This. Bolint =false; }}, $get:function(){ varthis = This; return function(num1,num2) {returnThis.bolint? Math.Round (Math.random () * (NUM2-NUM1)) + num1:Math.random () * (NUM2-NUM1) +NUM1; }; } }; }); M1.config ([' Myrandomnumprovider ',function(Myrandomnumprovider) {Myrandomnumprovider.int(false); }]); M1.controller (' Aaa ', [' $scope ', ' myrandomnum ',function($scope, Myrandomnum) {Console.log (Myrandomnum (-3,6) );
here}]);
Attention:
The special thing about the provider is that it can be configured at module start-up for a special purpose
M1.config ([' Myrandomnumprovider ',function(myrandomnumprovider) { myrandomnumprovider. int (false); }]);
Reference: http://ju.outofmemory.cn/entry/121904
Factory Service provide custom services