1.创建指令的4种方式(ECMA)

var appModule = angular.module('app', []);
appModule.directive('hello', function(){
return {
/**
* E 元素
* C class
* M 注释 directive:hello
* A 属性 默认
*/
restrict: 'ECMA',
template: '<div>hello world.</div>',
replace: true
};
});

2.指令的多种调用方式

<!-- 定义指令,匹配过程(去掉'x-'、'data-'、转化':', ',' , '-', '_-'为驼峰式) -->

<my-hello></my-hello>
<div my-hello></div>
<!-- directive:my-hello -->
<div class="my-hello"></div>
<my:hello></my:hello>
<my_hello></my_hello>
<data-my-hello></data-my-hello>
<x-my-hello></x-my-hello>

3.service 与 controller、directive交互

<button add-book-button>Add book</button>

<ul ng-controller="books.list">
<li ng-repeat="i in books">书名:{{i.title}},作者:{{i.author}}</li>
</ul>
var appModule = angular.module('app', []);

    // service 单例,共享数据
appModule.service('Book', ['$rootScope', function($root){ var service = {
books: [
{
"title": "书名1",
"author": "作者1"
},
{
"title": "书名2",
"author": "作者2"
}
],
addBook: function(book){
service.books.push(book); // 给root下所有的books.update派发事件
$root.$broadcast('books.update');
}
}; return service;
}]); // 在控制器里使用Book服务
appModule.controller('books.list', ['$scope', 'Book', function(scope, Book){ scope.books = Book.books; scope.$on('books.update', function(){
scope.$apply(function(){
scope.books = Book.books;
});
}); }]); // 在指令里使用Book服务
appModule.directive('addBookButton', ['Book', function(Book){
return {
restrict: 'A',
link: function(scope, el){
var n = 0;
el.bind('click', function(){
Book.addBook({
"title": "新书"+(++n),
"author": "新作者"+n
});
});
}
};
}]);

4.controller 与 controller交互

<!-- 控制器嵌套 -->
<div ng-controller="Ctrl1Parent">
<p>{{text}}</p>
<button ng-click="changeText()">控制器嵌套Parent</button>
<div ng-controller="Ctrl1Child">
<p>{{text}}</p>
<button ng-click="changeText()">控制器嵌套Child</button>
</div>
</div> <!-- 控制器嵌套,向上传播 -->
<div ng-controller="Ctrl2Parent">
<p>{{text}}</p>
<div ng-controller="Ctrl2Child">
<button ng-click="changeText()">向上传播</button>
</div>
</div> <!-- 控制器嵌套,向下传播 -->
<div ng-controller="Ctrl3Parent">
<button ng-click="changeText()">向下传播</button>
<div ng-controller="Ctrl3Child">
<p>{{text}}</p>
</div>
</div> <!-- 控制器嵌套,兄弟传播 -->
<div ng-controller="Ctrl4Parent">
<div ng-controller="Ctrl4Child">
<p>{{text}}</p>
<button ng-click="changeText()">兄弟传播</button>
</div>
<div ng-controller="Ctrl4Child">
<p>{{text}}</p>
<button ng-click="changeText()">兄弟传播</button>
</div>
</div> <!-- 服务 -->
<div ng-controller="Ctrl5Main">
<input type="text" ng-model="test" />
<div ng-click="add()">add</div>
</div>
<div ng-controller="Ctrl5Side">
<div ng-click="get()">get {{name}}</div>
</div>
var appModule = angular.module('app', []);

    // 控制器与控制器交互 -> 控制器嵌套
appModule.controller('Ctrl1Parent', ['$scope', function(scope){
scope.text = 'hello';
scope.changeText = function(){
scope.text = 'parent';
};
}]);
appModule.controller('Ctrl1Child', ['$scope', function(scope){
scope.changeText = function(){
scope.text = 'child';
};
}]); /**
* $on 绑定事件
* $emit 向上传递事件(冒泡)
* $boardcast 向下传递事件(捕获)
*/
// 控制器与控制器交互 -> 事件传播(控制器嵌套,向上传播)
appModule.controller('Ctrl2Parent', ['$scope', function(scope){
scope.text = 'parent';
scope.$on('changeText', function(ev, text){
scope.text = text;
});
}]);
appModule.controller('Ctrl2Child', ['$scope', function(scope){
scope.changeText = function(){
scope.$emit('changeText', 'child');
};
}]); // 控制器与控制器交互 -> 事件传播(控制器嵌套,向下传播)
appModule.controller('Ctrl3Parent', ['$scope', function(scope){
scope.text = 'parent';
scope.changeText = function(){
scope.$broadcast('changeText', 'child');
};
}]);
appModule.controller('Ctrl3Child', ['$scope', function(scope){
scope.$on('changeText', function(ev, text){
scope.text = text;
});
}]); // 控制器与控制器交互 -> 事件传播(同级传播)
appModule.controller('Ctrl4Parent', ['$scope', function(scope){
// 绑定一个changeTextAll事件,它被触发时会向子作用域发布changeTextExe
scope.$on('changeTextAll', function(){
scope.$broadcast('changeTextExe');
});
}]);
appModule.controller('Ctrl4Child', ['$scope', function(scope){
scope.text = 'parent'; // 触发父控制器的changeTextAll事件,得到changeTextExe
scope.changeText = function(){
scope.$emit('changeTextAll');
}; // changeTextExe发生
scope.$on('changeTextExe', function(){
scope.text = 'changeTextExe';
});
}]); // 以服务的方式交互
appModule.factory('instance', function(){
return {};
});
appModule.controller('Ctrl5Main', function($scope, instance){
$scope.add = function() {
instance.name = $scope.test;
};
});
appModule.controller('Ctrl5Side', function($scope, instance){
$scope.get = function() {
$scope.name = instance.name;
};
});

【回忆1314】第一次用AngularJS的更多相关文章

  1. 第一次用angularJS做后台管理点滴

    很早以前就大概看过一点angualrjs,但是没有项目,一直没有进行下去,就是干巴巴的看着,过了一段时间发现什么也不记得了. 来yulebaby我的第一个后台管理是用easyui做的,做完那个以后发现 ...

  2. 【回忆1314】抽奖之Flash大转盘

    1.搭建JS与Flash互通的环境 function thisMovie(movieName){ if (window.document[movieName]) { return window.doc ...

  3. 【回忆1314】回忆之placeholder

    直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charse ...

  4. 第一次用AngularJS

    1.创建指令的4种方式(ECMA) var appModule = angular.module('app', []); appModule.directive('hello', function() ...

  5. 从jquery里的$.ajax()到angularjs的$http

    jquery中对ajax的使用做了很多封装,使得我们使用习惯了,反而并不大清楚在请求过程中的一些细节. 在第一次使用angularjs的$http时,后台一直接受不到前端请求的数据,于是小小研究了一下 ...

  6. 使用 AngularJS 和 Electron 构建桌面应用

    GitHub 的 Electron 框架(以前叫做 Atom Shell)允许你使用 HTML, CSS 和 JavaScript 编写跨平台的桌面应用.它是io.js 运行时的衍生,专注于桌面应用而 ...

  7. requirejs+angularjs搭建SPA页面应用

    AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核 ...

  8. AngularJS快速入门01-基础

    记得第一次听说AngularJS这项很赞的Web的前端技术,那时还是2014年,年中时我们我的一个大牛兄弟当时去面试时,被问到了是否熟悉该技术,当时他了解和使用的技术比较多.我们询问他面试情况时,他给 ...

  9. AngularJs $http.post 数据后台获取不到数据问题 的解决过程

    第一次使用 AngularJs 的 $http 模块的时候,遇到过后台获取不到前台提交数据的问题,检查代码没有发现问题,先上代码. js 代码 angular.module("newsApp ...

随机推荐

  1. VS中的预先生成事件和后期生成事件

    原文:VS中的预先生成事件和后期生成事件 在C#开发中,有时候需要在程序编译之前或之后做一些操作. 要达到这个目的,可以使用Visual Studio中的预先生成事件和后期生成事件. 下图是一个简单例 ...

  2. 【HDOJ】1243 反恐训练营

    LCS. /* 1243 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...

  3. 【转】android 中如何限制 EditText 最大输入字符数

    原文网址:http://blog.csdn.net/fulinwsuafcie/article/details/7437768 方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:andr ...

  4. 【动态规划】XMU 1588 01序列计数

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1588 题目大意: 给n1个0和n2个1,连续的0不超过k1个,连续的1不超过k2个.问 ...

  5. 《Ruby语言入门教程v1.0》学习笔记-01

    <Ruby语言入门教程v1.0> 编著:张开川 邮箱:kaichuan_zhang@126.com 想要学习ruby是因为公司的自动化测试使用到了ruby语言,但是公司关于ruby只给了一 ...

  6. Jmeter安装设置

    Linux: 其实下载之后就可以用,不过为了能在命令行直接敲入”jmeter"就可以调出Jmeter程序,还需要进行如下设置. 1. Download apache-jmeter-2.12. ...

  7. ubuntu navicat110 for mysql 装配与破解

    安装:解压后即可用.目录下的start_navicat文件为可执行文件. 破解:(找过好几个注册码都不能用,注册码生成器都是windows平台的) ----第一次执行start_navicat时,会在 ...

  8. UVA 140 (13.07.29)

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anorderi ...

  9. Robotium API -- 除click/clickLong外的其他操作

    拖动操作 void drag (float fromX, float toX, float fromY, float toY, int stepCount) 选定两个位置,进行拖动操作(这里的拖动操作 ...

  10. Gradle 用法总结

    用过android studio的对gradle应该都不陌生了,gradle文件的基本配置大同小异,略做了解使用应该是没什么问题了.但是深入细致的了解一下对于理解项目还是很有帮助的,尤其是遇到一些配置 ...