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. Cacti添加threshold、monitor和setting

    Cacti版本:Version 0.8.8b 一.插件介绍: monitor:通过简单明了的图标提供服务器的运行状态 settings:给不同的插件提供一些共用的信息,如邮件信息,dns信息thold ...

  2. 【转】iOS开发系列--数据存取

    原文: http://www.cnblogs.com/kenshincui/p/4077833.html#SQLite 概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储 ...

  3. 【转】 iOS开发数据库篇—SQLite简单介绍

    开始学SQLite啦, 原文: http://www.cnblogs.com/wendingding/p/3868893.html iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中 ...

  4. Objective-C学习篇02—封装

    面向对象的三大特性:封装.继承和多态 封装目的就是将数据隐藏起来,外界只能通过这个类的方法(接口)才能访问或者设置里面的数据,不可以在外部直接修改或者访问里面的数据,通常使用方法来达到封装一个类的目的 ...

  5. 【USACO 3.1.2】总分

    [描述] 学生在我们USACO的竞赛中的得分越多我们越高兴.我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助.我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是 ...

  6. C#:装箱和拆箱相关知识整理

    1.装箱和拆箱是一个抽象的概念   2. 装箱是将值类型转换为引用类型 ;   拆箱是将引用类型转换为值类型   利用装箱和拆箱功能,可通过允许值类型的任何值与Object 类型的值相互转换,将值类型 ...

  7. 在VMware安装Centos再安装Oracle数据库(个人学习使用)

    打开VMware 选择稍后安装 自定义安装 小生安装的是64位的Centos 给虚拟机设置名称和安装位置 设置虚拟机打处理器并分配内存(oracle12G我建议内存为2G以上) 网络类型选择仅主机模式 ...

  8. 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。

    引用里找到Microsoft.Office.Interop.Word右键属性 在嵌入互操作类型里,选上False就行了.

  9. [Python笔记]第八篇:模块

    本篇主要内容:python常用模块用法介绍 什么是模块 模块,用一大段代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性 ...

  10. C++学习笔记2——引用

    1. int ival = 1; int &refVal = ival; //引用必须初始化,且类型严格匹配 2. int ival = 1; int &refVal = ival; ...