zoukankan      html  css  js  c++  java
  • [AngularJS] Services, Factories, and Providers -- value & Providers

    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;
                        }
    
                    };
  • 相关阅读:
    2019-2020-1学期 20192413 《网络空间安全专业导论》第九周学习总结
    2019-2020-1学期 20192413 《网络空间安全专业导论》第八周学习总结
    175210《网络对抗技术》Exp9 Web安全基础
    175210闵天 《网络对抗技术》Exp8 Web基础
    175210《网络对抗技术》Exp7 网络欺诈防范
    175210课设个人报告
    175210 Exp6 MSF基础应用
    175210课设第三次报告
    175210 《网络对抗技术》 Exp5 信息搜集与漏洞扫描
    175210课设第二次报告
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5045307.html
Copyright © 2011-2022 走看看