Factory Service provide custom services

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.