我也谈 AngularJS 怎么使用Directive, Service, Controller
原文地址:http://sunqianxiang.github.io/angularjs-zen-yao-shi-yong-directiveservicecontroller.html
其转自大漠穷秋,这是对于初学者来说 讲的非常好的入门文
在有关于 Directive,Service,Controller 时候先看一下图
这是我画出来帮助理解的,并不代表angularjs走这种逻辑。 不过在本例的确是这样的。 ========= 首先你需要先去定义你的module.
var Mymodule = angular.module('My.Alex.module', []);
html标记也记得修改哦
<html ng-app="My.Alex.module">
在这里我们先定义Service服务,本例是一个简单的图片添加的模式,数据跟add方法都在Service。这样有助于解耦。
Mymodule.service( 'Book',[ '$rootScope',function($rootScope){
var service = {
books: [
{ title: "Magician", author: "Raymond E. Feist" },
{ title: "The Hobbit", author: "J.R.R Tolkien" }
],
addBook: function(book){
service.books.push(book);
$rootScope.$broadcast('books.update');
}
}
return service;
}]);
在这里可以看出我定义了一个Book服务, 然后我现在要在controller添加他,来使用Book里面的数据。
Mymodule.controller("books.list",['$scope','Book',function(scope,Book){
scope.$on('books.update',function(event){
scope.books = Book.books;
scope.$apply(); #必须加,不然数据不更新
});
scope.books = Book.books;
}]);
这里在controller里面定义服务更新的方法,其主要是更新scope. $broadcast 跟$on 对应使用
module.directive( "addBookButton", [ 'Book', function( Book ) {
return {
restrict: "A",
link: function( scope, element, attrs ) {
element.bind( "click", function() {
Book.addBook( { title: "Star Wars", author: "George Lucas" } );
});
}
}
}]);
然后我添加一个directive来让button可以添加书本。 这是一个bind的过程,剩下只要添加 到body里面就完成了
<body ng-controller="books.list">
<li ng-repeat="book in books">
<ul>{{book.title}}, {{book.author}}</ul>
</li>
<button add-book-button>Add book</button>
</body>
至此,可以得到一个上面所画的一个基本逻辑代码。 其主要部分也就这4块。其实service还可以传递给filter。
我也谈 AngularJS 怎么使用Directive, Service, Controller的更多相关文章
- AngularJS中使用Directive、Controller、Service
AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可能不是太熟悉. (1)Directive 指令 (2)Controller 控制器 (3)Service ...
- AngularJS:何时应该使用Directive、Controller、Service?
AngularJS:何时应该使用Directive.Controller.Service? (这篇文章你们一定要看,尤其初学的人,好吗亲?) 大漠穷秋 译 AngularJS是一款非常强大的前端MVC ...
- 再谈angularJS数据绑定机制及背后原理—angularJS常见问题总结
这篇是对angularJS的一些疑点回顾,是对目前angularJS开发的各种常见问题的整理汇总.如果对文中的题目全部了然于胸,觉得对整个angular框架应该掌握的七七八八了.希望志同道合的通知补充 ...
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
- 何时应该使用Directive、Controller、Service?
AngularJS:何时应该使用Directive.Controller.Service? 大漠穷秋 译 AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可 ...
- AngularJs学习笔记--Managing Service Dependencies
原版地址:http://docs.angularjs.org/guide/dev_guide.services.managing_dependencies angular允许service将其他ser ...
- angularjs中的directive scope配置
angularjs中的directive scope配置 定义directive其中重要的一环就是定义scope,scope有三种形式: 默认的scope,DOM元素上原有的scope scope: ...
- angularJS中directive与controller之间的通信
当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...
- AngularJS入门心得1——directive和controller如何通信
粗略地翻了一遍<JavaScript DOM编程艺术>,就以为可以接过AngularJS的一招半式,一个星期过去了,我发现自己还是Too Young,Too Simple!(刚打照面的时候 ...
随机推荐
- 新学的js精集
逻辑运算符比较 逻辑与和逻辑或这两个逻辑运算符它不单单会输出布尔型数据 1.只要"||"前面为false,无论"||"后面是true还是false,结果都返回& ...
- c语言-猜数字游戏
#include <stdio.h> #include <stdlib.h> int top(); int input(); void main() { ; int numbe ...
- GITLAB管理自己的私有源码
github是很好的公开源码管理器,但是,私有项目,需要付费才行,比较郁闷,特别是个人工作者 gitlab(英文我不咋滴),上貌似允许1000个私有项目,其他的权限,还没怎么看,估计简单的项目 ...
- hdu_5761_Rower Bo(xjb猜公式)
题目链接:hdu_5761_Rower Bo 题意: 让你求一个物理问题 题解: xjb猜公式,由题目样例可得,答案为8/7,然后我们可以xjb猜出公式为v1*a/(v1*v1-v2*v2),然后特判 ...
- Codeforces Round #364 (Div. 2) E. Connecting Universities
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- 转载-SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart
[原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart 摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有 ...
- shell:crontab
crontab */1 * * * * (cd /home/q/system/project; /usr/bin/lockf -t 0 /tmp/discuz_bbs_audit.lock /usr/ ...
- PHP:preg_replace
关于preg_match: http://www.cnblogs.com/helww/p/3466720.html 关于preg_match_all:暂时没有完整的 preg_replace_call ...
- 【我与一道水题的抗争之路】 哈理工2323 Emirp(反素数)
题目: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2323 1,打表的姿势不对. ...
- Spring Security3实现,权限动态获取
Spring Security3实现,权限动态获取 原文 http://blog.csdn.net/yangwei19680827/article/details/9359113 主题 网络安全Sp ...