第一次用AngularJS
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;
};
});
第一次用AngularJS的更多相关文章
- 第一次用angularJS做后台管理点滴
很早以前就大概看过一点angualrjs,但是没有项目,一直没有进行下去,就是干巴巴的看着,过了一段时间发现什么也不记得了. 来yulebaby我的第一个后台管理是用easyui做的,做完那个以后发现 ...
- 【回忆1314】第一次用AngularJS
1.创建指令的4种方式(ECMA) var appModule = angular.module('app', []); appModule.directive('hello', function() ...
- 从jquery里的$.ajax()到angularjs的$http
jquery中对ajax的使用做了很多封装,使得我们使用习惯了,反而并不大清楚在请求过程中的一些细节. 在第一次使用angularjs的$http时,后台一直接受不到前端请求的数据,于是小小研究了一下 ...
- 使用 AngularJS 和 Electron 构建桌面应用
GitHub 的 Electron 框架(以前叫做 Atom Shell)允许你使用 HTML, CSS 和 JavaScript 编写跨平台的桌面应用.它是io.js 运行时的衍生,专注于桌面应用而 ...
- requirejs+angularjs搭建SPA页面应用
AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核 ...
- AngularJS快速入门01-基础
记得第一次听说AngularJS这项很赞的Web的前端技术,那时还是2014年,年中时我们我的一个大牛兄弟当时去面试时,被问到了是否熟悉该技术,当时他了解和使用的技术比较多.我们询问他面试情况时,他给 ...
- AngularJs $http.post 数据后台获取不到数据问题 的解决过程
第一次使用 AngularJs 的 $http 模块的时候,遇到过后台获取不到前台提交数据的问题,检查代码没有发现问题,先上代码. js 代码 angular.module("newsApp ...
- AngularJS 和 Electron 构建桌面应用
译]使用 AngularJS 和 Electron 构建桌面应用 原文: Creating Desktop Applications With AngularJS and GitHub Electro ...
- AngularJS 之1-初识
摘要:本文主要记录第一次接触AngularJS的笔记,现在在我面前就是一张白纸+一点简单的html知识. 1.首先在<head>中加 <script src="一个网址(具 ...
随机推荐
- jvm调优命令
JDK监控和故障jps,jstat,jmap,jhat,jstack.jinfo jps ,JVM Process Status Tool,显示指定系统内所有的HotSpot虚拟机进程. jstat ...
- springboot 和spring cloud 博客分享
spring boot 知识点总结 天狼星 https://www.cnblogs.com/wjqhuaxia/p/9820902.html spring cloud 知识点总结 姿势帝 https: ...
- GPIO引脚速度的应用匹配
GPIO 引脚速度: GPIO 引脚速度又称输出驱动电路的响应速度:(芯片内部在I/O口的输出部分安排了多个响应速度不同的输出驱动电路,用户可以根据自己的需要选择合适的驱动电路,通过选择速度来选择不同 ...
- session.flush与transaction.commit
以session的save方法为例来看一个简单.完整的事务流程,如下是代码片段: ---------------------------- Session session = sessionFacto ...
- react项目实现多语言切换
网站的语言切换功能大家都见过不少,一般都是一个下拉框选择语言,如果让我们想一下怎么实现这个功能,我相信大家都是有哥大概思路,一个语言切换的select,将当前的选择的语言存在全局,根据这个语言的key ...
- go进阶--测试
目录 1.单元测试 1.1项目结构 1.2 源代码文件 1.3 单元测试 1.4 执行测试 1.5 单元测试总结 2.性能测试 2.1 项目结构 2.2 源码文件 2.3 测试文件 2.4 执行测试 ...
- docker容器 如何精简镜像减小体积
写在前面 我们在上篇<Docker容器 关于镜像构建的安全问题>一起学习了如何构建一个基于安全的镜像,这篇小作文我们会学习镜像构建的另一个关键性问题,为何别人打造的镜像只有10MB而我的有 ...
- 自己封装一个Object.freeze()方法
1.遍历所有属性和方法 2.修改遍历到的属性的描述 3.Object.seal() Object.defineProperty(Object,'freezePolyfill',{ value:func ...
- 主要DL Optimizer原理与Tensorflow相关API
V(t) = y*V(t-1) + learning_rate*G(x) x(t) = x(t-1) - V(t) 参考:https://arxiv.org/pdf/1609.04747.pdf DL ...
- 24点游戏(24 game)的C++编程求解实现
什么是24点游戏 24点游戏,英文叫做24 game,是对给定的4个非负整数进行加减乘除运算,要求每个数都要被用到且仅用到一次,并得到最终的运算结果为24.比如3.8.3.8这四个数,可以找出唯一的一 ...