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. Chrome扩展与用户隐私

    转载自https://www.imququ.com/post/chrome-extensions-and-user-privacy.html   Google Chrome浏览器应该早就是大家的默认了 ...

  2. Objective-C中的协议(Protocol)和类别(Category)

    1.什么是协议? 2.协议与类别的声明和使用 1.什么是协议? 在Objective-C中,不支持多继承,即不允许一个类有多个父类,但是OC提供了类似的实现方法,也就是协议.协议有点类似于Java里的 ...

  3. Mybatis3.0防止SQL注入

    一.什么是SQL注入 引用搜狗百科: SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如很多影视网站泄露VIP会员密码大 ...

  4. 自动化构建工具gulp

    1.优点 1.1 易于使用 通过代码优于配置的策略,gulp让简单的任务简单,复杂的任务可管理 1.2 构建快速 利用node.js流的威力,你可以快速构建项目并减少频繁的IO操作 1.3 插件高质 ...

  5. javascript读取xml的方法【转载】

    jquery读取xml文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  6. php 带cookie采集某页面

    <?php error_reporting(0); define("temp_dir", dirname(__FILE__)."/Public/"); d ...

  7. php 用于绘图使用的颜色数组

    $colorArr = array(0x912CEE, 0x99ff00, 0x312520, 0x801dae, 0x25f8cb, 0xCC3333, 0x808080, 0xa29b7c, 0x ...

  8. python ATM购物程序

    需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...

  9. SCJP_104——题目分析(2)

    3. public class IfTest{ public static void main(String args[]){ int x=3; int y=1; if(x=y) System.out ...

  10. 构建高可用web站点学习(三)

    分布式的构建 做为网站访问的生命线(数据访问),当然也可以采用分布式的方法来减轻单台服务器的访问压力.之前有讲过Memcached的分布式,但是Memcached服务器互不通信,所以我们也提过redi ...