This lesson describes what is really happening when you use the angular
factory and how you can make your factories even more dynamic in creation.
This gets further into the internals of AngularJS by showing you how a factory is built dynamically for you and how they have reduced the plumbing you need to make applications.

Factory:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>Egghead Videos</title>
<link rel="stylesheet" href="vendor/foundation/foundation.min.css">
</head>
<body> <div ng-app="app" ng-controller="AppCtrl">
<h1 class="panel">{{title}}</h1>
</div> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script type="text/javascript" src="app/js/main.js"></script>
</body>
</html>
var app = angular.module("app", []);

app.factory("game", function() {
return {
title: "StarCraft"
}
}) app.controller("AppCtrl", function($scope, game) {
$scope.title = game.title
});

"app.factory" is just simply short-hand for using this "provide object":

/**
* Created by Answer1215 on 12/27/2014.
*/
var app = angular.module("app", []); app.config(function($provide) {
$provide.factory("game", function() {
return{
title: "StarCraft"
}
})
}) app.controller("AppCtrl", function($scope, game) {
$scope.title = game.title
});

"Factory" is really just shorthand for the provider which sets up a more generic provider which returns things or objects that have "get" functions:

/**
* Created by Answer1215 on 12/27/2014.
*/
var app = angular.module("app", []); app.config(function($provide) {
$provide.provider("game", function() {
return{
$get: function() {
return{
title: "StarCraft"
}
}
}
})
}) app.controller("AppCtrl", function($scope, game) {
$scope.title = game.title
});

If you want to set up something like game provider and set the type of game:

/**
* Created by Answer1215 on 12/27/2014.
*/
var app = angular.module("app", []); app.provider("game", function() { var type;
return{
setType: function(title) {
type = title;
},
$get: function() {
return{
title: type + "Craft"
}
}
}
}) app.config(function(gameProvider) {
gameProvider.setType("War");
}) app.controller("AppCtrl", function($scope, game) {
$scope.title = game.title
});

[AngularJS] Provider的更多相关文章

  1. angularjs provider 供应商服务

    今天学习了angularjs的provider的供应商服务,写了个例子(自定义供应商服务,也可使用angularjs内部提供的服务) var starterApp = angular.module(' ...

  2. AngularJS 中的 factory、 service 和 provider区别,简单易懂

    转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...

  3. angularJs 解析factory、service、provider

    了解angular js factory可以认为是设计模式中的工厂方法,就是你提供一个方法,该方法返回一个对象的实例:对于angularJs的factory,就是先定义一个对象,给这个对象添加属性和方 ...

  4. AngulaJS实战总结, 带你进入AngularJS世界(待续)

    使用AngularJS  进行Hybrid App 开发已经有一年多时间了,这里做一个总结. 一.AngularJS 初始化加载流程 1.浏览器载入HTML,然后把它解析成DOM.2.浏览器载入ang ...

  5. 使用AngularJS 进行Hybrid App 开发已经有一年多时间了,这里做一个总结

    一.AngularJS 初始化加载流程 1.浏览器载入HTML,然后把它解析成DOM.2.浏览器载入angular.js脚本.3.AngularJS等到DOMContentLoaded事件触发.4.A ...

  6. angularjs 学习小结

    1.过滤器的使用 <!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> ...

  7. AngularJS 初始化加载流程

    一.AngularJS 初始化加载流程 1.浏览器载入HTML,然后把它解析成DOM.2.浏览器载入angular.js脚本.3.AngularJS等到DOMContentLoaded事件触发.4.A ...

  8. AngulaJS实战

    AngulaJS实战总结, 带你进入AngularJS世界(待续)   使用AngularJS  进行Hybrid App 开发已经有一年多时间了,这里做一个总结. 一.AngularJS 初始化加载 ...

  9. ionic service

    当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一定要早点意识到,controller 这一层应该很薄:也就是说,应用里大部分的业务逻辑和持久化 ...

随机推荐

  1. Canvas处理头像上传

    未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...

  2. HDU 5389 Zero Escape

    题意:有一些人,每人拿一个号码,有两个门,门的值分别为A和B,要求把人分成两堆(可以为空)一堆人手持号码之和的数字根若等于A或者B就可以进入A门或者B门,要求两堆人分别进入不同的门,求有几种分配方式, ...

  3. DBCP连接池原理分析及配置用法

    DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...

  4. UI控件之 ScrollView垂直滚动控件 和 HorizontalScrollView水平滚动控件的使用

    1. ScrollView 垂直滚动控件的使用 ScrollView控件只是支持垂直滚动,而且在ScrollView中只能包含一个控件,通常是在< ScrollView >标签中定义了一个 ...

  5. XNA Game Studio 4.0 Programming 随便读,随便记 “Game Class”

    XNA 中的 Game 类,是所有神奇事情发生的地方.几乎游戏中所有的事情都由它来操办. 它是项目中的王者,让我们深入窥探一番: 虚方法 Game 本身从众多其它地方继续了许多能力才能完成游戏中的事情 ...

  6. c++ 概念及学习/c++ concept&learning(一)

    学习过计算机组成原理就会知道,处理器会从主存中取得指令,然后进行解释执行.而他们的交流方式是以二进制方式进行的,也就是他们只能识别1和0 :其实计算机是不知道1和0的,现在的实现方式是以高电压与低电压 ...

  7. RestTemplate中文乱码问题

    使用RestTemplate传输带有图片的表单时,需要对表单中的中文参数进行URL编码, eg :URLDecoder.decode(name);               // 使用默认的解码   ...

  8. CSS选择器的特殊性

    在我们为元素添加样式的时候,或多或少会出现一个元素会有几个不同规则的样式.有#id的,有.class,直接标签元素的,还有各种组合起来的选择器.那CSS到底如何解决这些冲突呢,我们这次专门来探讨一下. ...

  9. HDU 5833 Zhu and 772002 (高斯消元)

    Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...

  10. Spring Autowiring by AutoDetect

    In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...