问题描述
我也发现自己正在研究这个悬而未决的问题,但我也想知道找到答案是否重要?我用谷歌搜索了 $event
并查看了角度材料(至少是 API 文档左侧菜单中的项目)和材料文档,但没有找到任何参考.我只在 UI Router 文档中找到了对它的引用,表明它可以停止事件传播.
那么什么是$event
?为什么它被传递到 $mdDialog
中?如果我使用 ui-router
onEnter
,我需要它吗?
直接来自 docs:$event 对象是一个 jQuery 事件对象的实例当存在 jQuery 或类似的 jqLite 对象时.
在 angular-material 的情况下,它用于引用 dom 事件对象,例如在 $mdDialog
更新:
您必须将状态更改包装在 ng-click
事件中以获取 $event
对象并通过 $state.go 传递该事件对象()
:
<a ng-click="show($event)">添加活动</a>
然后配置您的状态:
.state("campaigns.add", {网址:/添加",解决: {事件:函数($stateParams){返回 $stateParams.event;}},onEnter:函数($mdDialog,$state){var ev = null;$mdDialog.show($mdDialog.alert().parent(angular.element(document.body)).title('这是一个警告标题').content('你可以在这里指定一些描述文本.').ariaLabel('警报对话框演示').好的,我知道了!').targetEvent(event)).then(function() {$state.go('完成');});}})
这是基于另一个问题的代码的工作演示:http://jsfiddle.net/f30wesj3/2/
I too find myself looking into this unanswered question but I also find myself wondering if finding an answer matters? I've googled $event
and looked through both angular material (well at least the items in the left hand menu of the API docs) and material docs and don't find any reference to it. I've only found a reference to it in the UI Router docs, suggesting it can stop event propagation.
so what is $event
? why is it passed into an $mdDialog
? do I need it if I'm using ui-router
onEnter
?
Directly from the docs: $event object is an instance of a jQuery Event Object when jQuery is present or a similar jqLite object.
In the case of angular-material it is used to reference dom event object, for instance a click's event object is used in $mdDialog
Update:
You will have to wrap your state change inside a ng-click
event to get the $event
object and pass that event object through $state.go()
:
<div ng-app="myApp" ng-controller="myController">
<a ng-click="show($event)">add campaign</a>
</div>
And then configure your state:
.state("campaigns.add", {
url: "/add",
resolve: {
event: function($stateParams) {
return $stateParams.event;
}
},
onEnter: function($mdDialog, $state) {
var ev = null;
$mdDialog.show(
$mdDialog.alert()
.parent(angular.element(document.body))
.title('This is an alert title')
.content('You can specify some description text in here.')
.ariaLabel('Alert Dialog Demo')
.ok('Got it!')
.targetEvent(event)).then(function() {
$state.go('done');
});
}
})
Here is a working demo based on the code from the other question: http://jsfiddle.net/f30wesj3/2/
这篇关于Angular Material 中的 $event 是什么,UI Router 是否需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!