事件(和js一样有冒泡和捕获)


<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script> <div ng-controller="parent">parent:{{detail}}
<div ng-controller="test">
myself:{{detail}}
<div ng-controller="child">child:{{detail}}</div>
<button ng-click="addparent()">addparent</button>
<button ng-click="addchild()">addchild</button>
</div>
</div>
<script>
var app = angular.module('app', [])
.controller('parent', ['$scope',function($scope) {
$scope.detail =1;
$scope.$on('add',function(){
$scope.detail +=1;
}); }]).controller('child', ['$scope',function($scope) {
$scope.detail =1;
$scope.$on('add',function(){
$scope.detail +=1;
}); }]).controller('test', ['$scope',function($scope) {
$scope.detail =1;
$scope.$on('add',function(){
$scope.detail +=1;
});
$scope.addparent=function(){
$scope.$emit('add');
};
$scope.addchild=function(){
$scope.$broadcast('add');
};
}]);
angular.bootstrap(document, ['app']);
</script>
</html>

从上面的代码可以看出$on来订阅一个事件,$emit触发的事件会冒泡处理,
$broadcast触发的事件会捕获,
系统还有很多自带的事件可以订阅,比如路由成功与否的$routeChangeError$routeChangeSuccess

ng-bind-html-unsafe(新版本中被移除,可以新建指令依赖ngSanitize)


<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular-sanitize.js"></script>
<div ng-controller="parent" ng-bind-html-unsafe="html"></div>
<script>
angular.module('app', ['ngSanitize']).config(['$compileProvider',
function($compileProvider) {
$compileProvider.directive({
ngBindHtmlUnsafe: function() {
return function(scope, element, attr) {
element.addClass('ng-binding').data('$binding', attr.ngBindHtmlUnsafe);
scope.$watch(attr.ngBindHtmlUnsafe,
function ngBindHtmlUnsafeWatchAction(value) {
element.html(value || '');
});
}
}
});
}]).controller('parent', ['$scope',
function($scope) {
$scope.html = "<span>aaa</span>";
}]);
angular.bootstrap(document, ['app']);
</script>
</html>

前面了解了指令的用法后,应该觉得很简单啦


Ⅶ.AngularJS的点点滴滴-- 事件的更多相关文章

  1. Ⅵ.AngularJS的点点滴滴-- 指令

    指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...

  2. Ⅴ.AngularJS的点点滴滴-- 资源和过滤

    资源ngResource(依赖ngResource模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ ...

  3. AngularJS HTML DOM& 事件

    AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令. ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性 <div ng-app= ...

  4. AngularJS 学习之事件

    1.ng-click指令:定义了AngularJS点击事件 <div ng-app="" ng-controller="myCtrl"> <b ...

  5. angularJS支持的事件

    AngularJS提供可与HTML控件相关联的多个事件.例如ng-click通常与按钮相关联.以下是AngularJS支持的事件. ng-click ng-dbl-click ng-mousedown ...

  6. angularjs的touch事件

    angularJs没有touch事件.这里提供一个touch指令. ngTouch.js "use strict"; angular.module("ngTouch&qu ...

  7. Ⅳ.AngularJS的点点滴滴-- 服务

    服务(Angularjs很多方法都是服务组成的) 1.使用service方法创建的单例服务 <html> <script src="http://ajax.googleap ...

  8. Ⅲ.AngularJS的点点滴滴-- 路由

    路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...

  9. Ⅱ.AngularJS的点点滴滴--缓存

    模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...

随机推荐

  1. 李洪强漫谈iOS开发[C语言-034]-程序的结构

  2. BZOJ1345: [Baltic2007]序列问题Sequence

    1345: [Baltic2007]序列问题Sequence Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 556  Solved: 274[Submi ...

  3. 【转】LINUX系统I/O复用技术之二:poll() -- 不错

    原文网址:http://www.cnblogs.com/alyssaCui/archive/2013/04/01/2993886.html poll poll或select为大部分Unix/Linux ...

  4. 【转】为ListView每个Item上面的按钮添加事件

    原文网址:http://blog.csdn.net/qq435757399/article/details/8256453 1.先看下效果图:        在这里仅供测试,我把数据都写死了,根据需要 ...

  5. POJ -- 2955

    Brackets Description We give the following inductive definition of a “regular brackets” sequence: th ...

  6. HDU-2562 奇偶位互换

    http://acm.hdu.edu.cn/showproblem.php?pid=2562 奇偶位互换 Time Limit: 3000/1000 MS (Java/Others)    Memor ...

  7. devi into python 笔记(六)正则表达式 原始字符串

    字符串函数replace: #string.replace: #字符串的replace方法:替换子串,不改变原来的字符串 s = "broad road" #打印出来会发现不单单是 ...

  8. 在Ubuntu 中安装eclipse, eclipse 文件已经下载好!

    If you've downloaded Eclipse from their official website, follow these steps for the installation. E ...

  9. usaco 奶牛集会 && 奶牛抗议

    奶牛集会 Description 约翰家的N头奶牛每年都会参加“哞哞大会” .哞哞大会是世界奶牛界的盛事.集会上 的活动很多,比如堆干草,跨栅栏,摸牛仔的屁股等等.当然,哞哞大叫肯定也包括在内. 奶牛 ...

  10. hdu 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4122 代码: #include<cstdio> #include<iostream& ...