angular js 事件
(1)angular js支持的事件
- ng-click
- ng-dbl-click
- ng-mousedown
- ng-mouseenter
- ng-mouseleave
- ng-mousemove
- ng-keydown
- ng-keyup
- ng-keypress
- ng-change
点击按钮,实现“测试”的显示与隐藏
测试
(3)自定义事件传播
1.前提
必须有controller嵌套,否则无法接收事件click me
这里四个controller的层级关系如下:
所有的事件都是从SelfCtrl发出
2.$broadcast
事件发送的方法:
$scope.$broadcast 事件的接收方法:
$scope.$on broadcast是从发送者
向他的子scope发送事件的一个方法,所以
父scope不会收到
angular.module('TestApp', ['ng'])
.controller('ParentCtrl', function($scope) {$scope.$on('to-child', function(e, d) {console.log('关我毛事');});
})
.controller('SelfCtrl', function($scope) {$scope.click = function () {$scope.$broadcast('to-child', 'haha');}
})
.controller('ChildCtrl', function($scope){$scope.$on('to-child', function(e, d) {console.log('I\' the child, I got it', d);});
})
.controller('BroCtrl', function($scope){$scope.$on('to-child', function(e, d) {console.log('关我毛事');});
})
;
3.$emit
事件发送的方法:
$scope.$broadcast
只有父scope能够收到
angular.module('TestApp', ['ng'])
.controller('ParentCtrl', function($scope) {$scope.$on('to-parent', function(e, d) {console.log('we are the parent, I got it', d);});
})
.controller('SelfCtrl', function($scope) {$scope.click = function () {$scope.$emit('to-parent', 'hehe');}
})
.controller('ChildCtrl', function($scope){$scope.$on('to-parent', function(e, d) {console.log('关我毛事');});
})
.controller('BroCtrl', function($scope){$scope.$on('to-parent', function(e, d) {console.log('关我毛事');});
})
;
4.说明
不管是$broadcast还是$emit,兄弟controller永远都不会收到事件
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
