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

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

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

    Creating a Service: Before actual create an angular service, first create a constructor in Javascrip ...

  2. [译]AngularJS Services 获取后端数据

    原文:ANGULARJS SERVICES – FETCHING SERVER DATA $http是AngularJS内置的服务,能帮助我们完成从服务端获数据.简单的用法就是在你需要数据的时候,发起 ...

  3. Part 19 AngularJS Services

    What is a service in AngularJSBefore we talk about what a service is in Angular. Let's talk about a ...

  4. Laravel 之Service Providers

    Service providers are the central place of all Laravel application bootstrapping. Your own applicati ...

  5. [AngularJS] Accessing Services from Console

    Using the Chrome console, you can access your AngularJS injectable services. This is down and dirty ...

  6. [Angular 2] 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 compone ...

  7. Custom Data Service Providers

    Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...

  8. [PHP] 浅谈 Laravel Authentication 的 guards 与 providers

    从文档的简单介绍上来讲,有一些抽象. 个人感觉,对于概念上的大多数不理解主要还是来自于 文档不是讲设计思路,而是实际操作. 查看英文文档,通常来说可以给你最准确的直觉,而本地翻译一般比较字面或者带有理 ...

  9. AngularJS 之 Factory vs Service vs Provider【转】

    英文原文:AngularJS: Factory vs Service vs Provider 当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一 ...

随机推荐

  1. excel - 统计字符个数综合案例

    本文通过一个综合的案例来介绍excel统计字符数的一些方法和思路,供大家参考和学习. 下图是一个excel数据源截图,我们逐一讲解不同条件的统计字符数. 第一,统计A2所有的字符数,不论是汉字和数字. ...

  2. 集成支付宝后出现LaunchServices: ERROR: There is no registered handler for URL scheme alipay

    原因如下: There's no problem with your implementation. All those warnings mean is the apps which each UR ...

  3. POJ3658Matrix( 双重二分+负数+死循环)

    POJ 3658 Matrix 双重二分,wa了一下午,实在不太明白为啥一写二分就会进入死循环. INF要设的大一些,本题设0x3f3f3f3f会wa. 本题有负数, 二分时(l+r)/2与(l+r) ...

  4. Cloud Foundry中 JasperReports service集成

    Cloud Foundry作为业界第一个开源的PaaS解决方案,正越来越多的被业界接受和认可.随着PaaS的发展,Cloud Foundry顺应潮流,充分发挥开源项目的特点,到目前为止,已经支持了大批 ...

  5. MPICH2在两台Ubuntu上安装(用mpd做进程管理)

    本文在经过大量的实验终于不负众望成功的在两台Ubuntu 12.04上部署MPI的一个小型集群,MPICH2所用版本为mpich2-1.4.1,下载地址:http://www.mcs.anl.gov/ ...

  6. angularJS中如何写服务

    服务的用途 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性 服务提供了把特定功能相关联的方法集中在一起的接口 如何创建服务 angularJS ...

  7. Thinkphp发布文章获取第一张图片为缩略图实现方法

    正则匹配图片地址获取第一张图片地址 此为函数 在模块或是全局Common文件夹中的function.php中 /** * [getPic description] * 获取文本中首张图片地址 * @p ...

  8. JS indexOf() lastIndexOf()与substring()截取字符串的区别

    1. String.IndexOf 方法 (value[,startIndex]) value:要查找的 Unicode 字符. 必选项startIndex:搜索起始位置.  可选项 不写从开头查找 ...

  9. HTML5 canvas生成图片马赛克特效插件

    HTML5 canvas生成图片马赛克特效插件 简要教程 这是一款使用html5 canvas来将图片制作成马赛克效果的js插件.该插件的灵感来自于美国肖像画家Chuck Close.已经有人使用这个 ...

  10. php 5.2 版本isset()方法小坑

    PHP 5.2.17p1 (cli) (built: May 28 2015 16:15:30)Copyright (c) 1997-2010 The PHP GroupZend Engine v2. ...