【备忘录】provider, factory, service, hello world example



var myApp = angular.module(‘myApp‘, []);

//service style, probably the simplest one
myApp.service(‘helloWorldFromService‘, function() {
    this.sayHello = function() {
        return "Hello, World!"
    };
});

//factory style, more involved but more sophisticated
myApp.factory(‘helloWorldFromFactory‘, function() {
    return {
        sayHello: function() {
            return "Hello, World!"
        }
    };
});

//provider style, full blown, configurable version
myApp.provider(‘helloWorld‘, function() {
    // In the provider function, you cannot inject any
    // service or factory. This can only be done at the
    // "$get" method.

    this.name = ‘Default‘;

    this.$get = function() {
        var name = this.name;
        return {
            sayHello: function() {
                return "Hello, " + name + "!"
            }
        }
    };

    this.setName = function(name) {
        this.name = name;
    };
});

//hey, we can configure a provider!
myApp.config(function(helloWorldProvider){
    helloWorldProvider.setName(‘World‘);
});

function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {

    $scope.hellos = [
        helloWorld.sayHello(),
        helloWorldFromFactory.sayHello(),
        helloWorldFromService.sayHello()];
}?

The value, factory, service, constant, and provider methods are all providers. They teach the Injector how to instantiate the Services.

1.the Value Recipe is the simplest case, where you instantiate the Service yourself and provide theinstantiated value to the injector.

2.The Factory recipe gives the Injector a factory function that it calls when it needs to instantiate the service. When called, the factory function creates and returns the service instance. The dependencies of the Service are injected as the functions‘s arguments. So using this recipe adds the following abilities:The Service recipe is almost the same as the Factory recipe, but here the Injector invokes aconstructor with the new operator instead of a factory function.

ability to use other services (have dependencies)

service initialization

delayed/lazy initialization

3.The Provider recipe is usually overkill. It adds one more layer of indirection by allowing you to configure the creation of the factory.

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

4.The Constant recipe is just like the Value recipe except it allows you to define services that are available in the config phase. Sooner than services created using the Value recipe. Unlike Values, they cannot be decorated using decorator.

 relative information on stackoverflow:  http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory

【备忘录】provider, factory, service, hello world example

时间: 2024-07-30 10:18:43

【备忘录】provider, factory, service, hello world example的相关文章

AngularJS中的provider,factory,service方法

使用$provide中的provider方法定义服务 前面已经知道了module的定义为angular.module(name,[requires],configFn);configFn参数是配置服务的.ng供服务的过程涉及它的依赖注入机制.AngularJS是用$provider对象来实现自动依赖注入机制的.$provide.provider是一种定义服务的方法.注入机制通过调用provider的$get方法,把得到的对象作为参数进行相关的调用. <!DOCTYPE html> <ht

angularjs factory,service,provider 自定义服务的不同

angularjs框架学了有一段时间了,感觉很好用.可以把angularjs的app理解成php的class,controller是控制器,而内置服务和自定义服务就可以理解成models了.angularjs的内置服务多,例如:$scope,$rootScope,$http,$q,$resource,$routeProvider等等,下面来说一下怎么自定义服务 一,factory,service,provider自定义服务,services.js 'use strict'; /* Service

angularjs 中 Factory,Service,Provider 之间的区别

本片文章是使用了 angularjs 中使用 service 在controller 之间 share 对象和数据 的code(http://jsfiddle.net/kn46u0uj/1/) 来进行演示 Factory,Service,Provider 之间的区别 1. Factory factory('dataService',function(){ return { golbal_sitename:"this is the shared value", sayHello:func

Angular之Providers (Value, Factory, Service and Constant )

官方文档Providers Each web application you build is composed of objects that collaborate to get stuff done.(每一个web应用都是由一些对象“组装”成的,这些对象共同合作,来完成特定的任务)These objects need to be instantiated and wired together for the app to work.(这些对象需要被实例化,然后“组装”在一起来使web应用能

Providers(Value, Factory, Service and Constant )

-------------------------------官方文档--------------------------------------------------------------------- Providers  AngularJS: Service vs provider vs factory Each web application you build is composed of objects that collaborate to get stuff done.(每一

AngularJS中几种Providers(Factory, Service, Provider)的区别

原文: http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/ 什么是Provider? AngularJS docs 是这样定义provider的: provider是一个对象, 它有一个$get()方法. injector 调用$get方法以此来创建一个service的实例. Provider还有一些其他的方法用来配置provider. AngularJS 使用 $provide 注册新的pro

factory service provide自定义服务

1.factory factory , 就是你提供一个方法, 该方法返回一个对象的实例, 对于 AngularJS 的 factory 来说, 就是先定义一个对象, 给这个对象添加属性和方法, 然后返回这个对象, 例如: var app = angular.module('MyApp', []); app.factory('MyFactory', function() { // define result object of factory. var result = {}; // add so

angular的service服务eg:value,constant,factory,service

app = angular.module("app",[]); app.value("realname","liyang");//可以改变 app.value("User",{name:"liyang" ,pwd:"123456"}) //可以直接绑定一个js对象 app.constant("baidu","www.baidu.com");//不可

Guice 学习(六)使用Provider注入服务( Provider Inject Service)

1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Service { public void execute(); } 2.定义实现类 package com.guice.providerInject; public class OneService implements Service { @Override public void execute() {