[AngularJS] Services, Factories, and Providers -- value & Providers
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的更多相关文章
- [AngularJS] Services, Factories, and Providers -- Service vs Factory
Creating a Service: Before actual create an angular service, first create a constructor in Javascrip ...
- [译]AngularJS Services 获取后端数据
原文:ANGULARJS SERVICES – FETCHING SERVER DATA $http是AngularJS内置的服务,能帮助我们完成从服务端获数据.简单的用法就是在你需要数据的时候,发起 ...
- Part 19 AngularJS Services
What is a service in AngularJSBefore we talk about what a service is in Angular. Let's talk about a ...
- Laravel 之Service Providers
Service providers are the central place of all Laravel application bootstrapping. Your own applicati ...
- [AngularJS] Accessing Services from Console
Using the Chrome console, you can access your AngularJS injectable services. This is down and dirty ...
- [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 ...
- Custom Data Service Providers
Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...
- [PHP] 浅谈 Laravel Authentication 的 guards 与 providers
从文档的简单介绍上来讲,有一些抽象. 个人感觉,对于概念上的大多数不理解主要还是来自于 文档不是讲设计思路,而是实际操作. 查看英文文档,通常来说可以给你最准确的直觉,而本地翻译一般比较字面或者带有理 ...
- AngularJS 之 Factory vs Service vs Provider【转】
英文原文:AngularJS: Factory vs Service vs Provider 当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一 ...
随机推荐
- 自定义android精美聊天界面
编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...
- C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体
//浏览图片 private void btnUp_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialo ...
- C# static 干货全解析
讲解顺序 背景 静态字段 静态函数 静态方法 疑问解答 背景 static来源 在编写类的时候,有时候需要类里面的某个成员具有唯一性,也就是,对所有的对象都保持只有一个的状态.比如创建个人信息,我们都 ...
- sublime text There are no packages 解决!
1.问题如下图 解决如下: 1.取得sublime.wbond.net的IPv4地址.在命令提示符中输入以下命令: ping sublime.wbond.net 获得 pv 4 ip 2.C ...
- jQuery firefox chrome IE 绑定mousewheel事件
$doc.on('mousewheel DOMMouseScroll',function(){ $htmlBody.stop(true); $goTop.stop(true); });
- CentOS 7 之Helloworld with c
其实我也不知道是为了啥, 到了现在这种年纪还想学习Linux下的C语言编程.因为我一直就傻傻地认为机会是垂青有准备的人,也一直呆呆地认为活到老学到老.现在Android这么火,各种终端如雨后春笋,而这 ...
- ar命令和nm命令(建库!)
ar 命令详解 今天,跟着我们的技术大牛学了不少东西,首先就是这个ar命令啦. 当我们的程序中有经常使用的模块,而且这些模块在其他程序中也会用到,为了实现代码重用减少软件开发周期,我们可以将它们生成库 ...
- jQuery实现checkbox全选反选及删除等操作
1.list.html 说明:用checkbox数组Check[]存放每一行的ID值 <div id="con"> <table width="100% ...
- 面向对象设计模式之Interpreter解释器模式(行为型)
动机:在软件构建过程中 ,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化.在这种情况下,将特定领域的问题表达为某种语法规则的句子,然后构建一个 ...
- jquery 插件大全
1.jquery.roundabout.js 超棒的左右3D旋转式幻灯片jQuery插件 2.jquery validate.js 验证表单 3.jquery ui插件 对话框 日期 4.lhgdia ...