[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;
                    }

                };
时间: 2024-10-19 10:01:43

[AngularJS] Services, Factories, and Providers -- value & Providers的相关文章

[AngularJS] Services, Factories, and Providers -- Service vs Factory

Creating a Service: Before actual create an angular service, first create a constructor in Javascript: //constructor function function DroidService() { this.name = ''; } DroidService.prototype.speak = function () { return "Hi I am " + this.name;

Part 19 AngularJS Services

What is a service in AngularJSBefore we talk about what a service is in Angular. Let's talk about a service in web development. If you have any experience developing web applications1. You might have heard about Web Services and WCF Services2. You mi

Angularjs Services

Services 的特性01.   Service 都是单例的02.   Service 由$injector 负责实例化03.   Service 在整个生命周期都存在,可以用来共享数据.04.   在需要使用的地方利用依赖注入机制注入Service05.   自定义的Service 需要在内置的Service后面.06.   内置Service的命名以$符号开头,自定义Service应该避免.

Custom Data Service Providers

Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which is responsible for interacting with the underlying Data Source on behalf of the Data Service. Data Services ships with some internal providers, and mak

[Angular 2] 5. Inject Service with "Providers"

In this lesson, we’re going to take a look at how add a class to the providers property of a component creates an actual providers. We’ll learn what a provider specifically does and how we can provide different dependencies with the same token. impor

[AngularJS] Accessing Services from Console

Using the Chrome console, you can access your AngularJS injectable services. This is down and dirty debugging, and can be a lifesaver in troubling times. You can get services/factories in console by using: var $injector = angular.element($0).injector

[PHP] 浅谈 Laravel Authentication 的 guards 与 providers

从文档的简单介绍上来讲,有一些抽象. 个人感觉,对于概念上的大多数不理解主要还是来自于 文档不是讲设计思路,而是实际操作. 查看英文文档,通常来说可以给你最准确的直觉,而本地翻译一般比较字面或者带有理解性的. https://laravel.com/docs/6.x/authentication#introduction 认证(Authentication)组件的配置是 config/auth.php,用于区分不同认证机制的行为,所以都是可以自定义的, 这是设计思路之一. Laravel 的认证

AngularJS 之 Factory vs Service vs Provider【转】

英文原文:AngularJS: Factory vs Service vs Provider 当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一定要早点意识到,controller 这一层应该很薄:也就是说,应用里大部分的业务逻辑和持久化数据都应该放在 service 里.我每天都会在 Stack Overflow 上看到几个同类的问题,关于如何在 controller 里保存持久化数据.这就不是 controller 该干的事.出于内存性

[转载]我看到的最好的解释AngularJs中Factory和Service和Provide不同

AngularJS: Factory vs Service vs Provider By Tyler On May 4, 2014 With 44 Comments In Technical When you first get started with Angular, you’ll naturally find yourself flooding your controllers and scopes with unnecessary logic. It’s important to rea