首页 > Web开发 > 详细

[AngularJS] Services, Factories, and Providers -- value & Providers

时间:2015-12-14 21:13:50      阅读:258      评论:0      收藏:0      [点我收藏+]

 

Creating a Value Object

Sometimes you have javascript object defined:

    //value object
    var droidValue = {
        name: ‘‘,
        speak: function () {
            return "Hi I am " + this.name;
        }
    };
    
    var droid = droidValue;
    droid.name = ‘bb-8‘;
    console.log(droid.speak());

 

If want to use this object in AngularJS, can use ‘value‘:

//angularjs
(function () {
    "use strict";

    //value object
    var droidValue = {
        name: ‘‘,
        speak: function () {
            return "Hi I am " + this.name;
        }
    };


    angular.module(‘app‘, [])
        .value(‘droid‘, droidValue)
        .controller(‘DroidController‘, DroidController)

    function DroidController(droid) {
        var droidCtrl = this;
        droid.name = ‘bb-8‘;
        droidCtrl.message = droid.speak();

    }


})();

 

Creating a Provider

//angularjs
(function () {
    "use strict";

    //module pattern (configurable per app)
    function droidProvider() {
        var greeting = ‘‘;
        return {
            configure: function (settings) {
                greeting = settings.greeting;
            },
            $get: function () {
                return {
                    name: ‘‘,
                    speak: function () {
                        return greeting + this.name;
                    }

                };
            }

        };
    }


    angular.module(‘app‘, [])
        .config(function (droidProvider) {
            droidProvider.configure({greeting: "Greetings I am "});

        })
        .provider(‘droid‘, droidProvider)
        .controller(‘DroidController‘, DroidController);

    function DroidController(droid) {
        var droidCtrl = this;
        droid.name = "ig-88";
        droidCtrl.message = droid.speak();

    }


})();

 

Important to understand:

  • Each provider should have a $get function
  • When you use config black to configure provider, it actually invoke droidProvider() function and then get the return object back
return {
            configure: function (settings) {
                greeting = settings.greeting;
            },
            $get: function () {
                return {
                    name: ‘‘,
                    speak: function () {
                        return greeting + this.name;
                    }

                };
            }

        };
  • When you inject provider into controller, it actually call the $get function inside the provider, and then return the object inside $get() function
return {
                    name: ‘‘,
                    speak: function () {
                        return greeting + this.name;
                    }

                };

 

[AngularJS] Services, Factories, and Providers -- value & Providers

原文:http://www.cnblogs.com/Answer1215/p/5045307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!