事件(和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>

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


最新文章

  1. 用C++实现Linux中shell的ls功能
  2. Javascript设置对象属性为&quot;只读&quot;
  3. jenkins 中 Poll SCM 和 Build periodically 的区别
  4. Linux环境下stl库使用(map)
  5. Java for LeetCode 050 Pow(x, n)
  6. iOS 面试题及答案
  7. [MODx] 1. Add Html5 template into the MODx
  8. shell 实例学习
  9. linux编程基础汇总贴
  10. Chapter 2 User Authentication, Authorization, and Security(11):在已还原的数据库中修正登录映射错误
  11. python中__init__和__new__的区别
  12. 安卓权限申请处理框架Android-UsesPermission
  13. Android的Base64的坑
  14. Linux查看服务器配置常用
  15. 关于unity3dGUI(uGUI)的一些自适应的收获,在这里跟大家分享一下
  16. 【Alpha 冲刺】8/12
  17. 【CC】Batman and Tree
  18. 部分Android或IOS手机拍照后照片被旋转的问题
  19. hasura-graphql 集成 pipelinedb 1.0.0
  20. iOS之面试题:腾讯三次面试以及参考思路

热门文章

  1. 【UVA 10369】 Arctic Network (最小生成树)
  2. go与rpc
  3. [转贴]PHP 开发者应了解的 24 个库
  4. Apache / PHP 5.x Remote Code Execution Exploit
  5. Chrome 浏览器地址栏直接搜索太慢的解决方案
  6. HDU 1162 Eddy&#39;s picture
  7. log4j的使用方法
  8. ubuntu64bits环境下搭建Opencl的环境
  9. Windows和Ubuntu双系统,修复UEFI引导的两种办法
  10. BAT-使用BAT方法判断网络启动EXE(快捷方式)