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. mac在 aliyun linux ecs实例上安装 jdk tomcat mysql

    用了一个ftp 工具 把 gz rpm 等 传递到ecs 上 -- 用这个Transmit 用ssh远程登录,然后依次安装 jdk tomcat  mysql 到 /usr/local/... 设置环 ...

  2. iOS中使用UIWebView与JS进行交互

    iOS中使用UIWebView与JS进行交互 前一段忙着面试和复习,这两天终于考完试了,下学期的实习也有了着落,把最近学的东西更新一下,首先是使用UIWebView与JS进行交互 在webView中我 ...

  3. UIView之常用方法

    UIView之常用方法 将一个视图添加为子视图,并使之在最上面显示 -(void)addSubView:(UIView *)view; 将指定子视图移动到顶部 -(void)bringSubViewT ...

  4. Cocos2dx 3.2 节点之间相互通信与设置触摸吞噬的方法

    实际开发中,我们经常会遇到这样的情况.我们有一个层layer1,这个层包含一个menu层,menu1层里又包含了一个节点按钮button1.现在需要实现一个效果:点击button1弹出一个对话框,这个 ...

  5. 高级I/O函数(3)-tee、fcntl函数

    tee函数使用 功能描述:tee函数在两个管道文件描述符之间复制数据,也是零拷贝操作.它不消耗数据,因此源文件描述符仍然可以用于后续的操作. 函数原型: #include <fcntl.h> ...

  6. Bit Map解析

    1. Bit Map算法简介 来自于<编程珠玑>.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空 ...

  7. 动态内存分配(new)和释放(delete)

    在之前我们所写过的程序中,所必需的内存空间的大小都是在程序执行之前就已经确定了.但如果我们需要内存大小为一个变量,其数值只有在程序运行时 (runtime)才能确定,例如有些情况下我们需要根据用户输入 ...

  8. 自动Telnet远程登陆命令

    有些时候,在面对开发机的时候,不断的telnet和不断的command自己的命令确实非常麻烦,需要一些自动测试或者自动部署的需求.然而面对telnet很多同学都跟我一样一开始觉得无法通过管道等传用户名 ...

  9. 如何在django中使用多个数据库

    http://blog.chinaunix.net/uid-16728139-id-4364343.html 

  10. Oracle的Net Configuration Assistant 配置

    在进行团队开发的时候,一般团队的每一个人只需要安装一个客户端即可,没有必要安装一个Oracle 数据库服务器,而数据库服务器是属于共享的,此时,我们就需要配置客户端.客户端的配置可以有以下两种方式:第 ...