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;
                        }
    
                    };
  • 相关阅读:
    sql server 获取存储过程,表值,标量函数的参数
    拼接枚举字符串
    存储过程的输出接受强类
    映射对象
    C# abstract,virtual 修饰符
    SqlSugar之SqlQueryDynamic返回值处理
    sql server 中数据库数据导入到另一个库中
    sql server 自增长显式添加值
    sql得到表中的列信息
    程序中MMap集合数据重复导致程序慢的情况
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5045307.html
Copyright © 2011-2022 走看看