What is $event in Angular Material and do I need it with UI Router?

强颜欢笑 提交于 2019-12-12 03:58:35

问题


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?


回答1:


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/



来源:https://stackoverflow.com/questions/32685557/what-is-event-in-angular-material-and-do-i-need-it-with-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!