ng-keyup inside directive is working but $event is undefined

435 views
Skip to first unread message

Diego Freitas

unread,
Jul 19, 2014, 3:54:11 PM7/19/14
to ang...@googlegroups.com
Hey guys,

I've been facing a problem to build a module for me.

I have this structure:

Calling my directive - passing a postComment function that is inside the moduleService which is inside the controller
<div input-directive class-name="globalInput" post-comment="moduleService.postComment($event, comment)" msg-placeholder="Redija um comentario..." class="mainComment"></div>

Directive 
.directive('inputDirective', function(){
return {
restrict: 'A',
scope: {
postComment: '&',
msgPlaceholder: '@',
className: '@'
},
template: "<textarea ng-model='comment' class='{{className}}' placeholder='{{msgPlaceholder}}' "+
"ng-keyup='postComment( {event: $event}, {comment: comment} )'></textarea>"
};
});

When the function is called I can see the comment object - but the event is undefined.

Service 
this.postComment = function(event, comment){
        //EVENT IS UNDEFINED
        }

Does anybody know what is happening?

Thanks in advance ! 

Eric Eslinger

unread,
Jul 19, 2014, 7:10:22 PM7/19/14
to ang...@googlegroups.com
Are you looking at event or event.event? Looks like you've attached $event to event.event there.

e


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Jens Melgaard

unread,
Jul 20, 2014, 5:08:14 AM7/20/14
to ang...@googlegroups.com
You are re-posting it as just 'event', not '$event'... your also passing 2 objects to the "inject-able function".  so you should either correct your directive template to: 

"ng-keyup='postComment( { event: $event, comment: comment} )' then consume it as post-comment="moduleService.postComment(event, comment)"

or

"ng-keyup='postComment( { $event: $event, comment: comment} )' 

Ciro Nunes

unread,
Jul 20, 2014, 9:42:34 PM7/20/14
to ang...@googlegroups.com
Can you provide a plunker isolating your issue? It would be a way easier to help you. =)

Diego Freitas

unread,
Jul 20, 2014, 10:01:46 PM7/20/14
to ang...@googlegroups.com
Hey, actually the issue is what Jens had said. The use of event instead of $event. Using event the event is defined. :)

Thanks all!!! Thanks Ciro too :)
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/O5yj3FN45c8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.


--


Diego Freitas
    (31)8884-8176

Conheça meu perfil no LinkedIn clicando aqui  Visualizar meu perfil no LinkedIn

Reply all
Reply to author
Forward
0 new messages