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;
                        }
    
                    };
  • 相关阅读:
    迅为-imx6ull开发板之C语言实现LED例程
    移植5.4内核到迅为I.MX6ULL开发板
    一文搞懂定制Ubuntu文件系统-基于迅为imx6开发板
    移植Linux-5.4+内核到4412开发板
    iTOP4412开发板Linux下多核处理器相关知识
    使用迅为IMX6ULL开发板第一个汇编实验(二)
    mplayer移植-迅为IMX6Q开发板
    使用迅为IMX6ULL开发板第一个汇编实验(一)
    网易2019秋招--翻转翻转
    百度2019秋招--混战世界
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5045307.html
Copyright © 2011-2022 走看看