0

I'm trying pass $event context to function at controller, but receive undefined param

My code:

directive('setting', function() {
 return {
  restrict: 'E',
  template: `
    <div class='dropdown setting'>
        <button type='button'>
            {{getSettingHTML($event)}}
        </button>
    </div>

};

and my controller function:

 $scope.getSettingHTML = function(e) {
   console.log(e);
 };

 // return e == undefined 

With {{getSettingHTML('xxx')}} I got right string.

How can I get the target which call to function?

Thanks!

7
  • please see this answer Commented Aug 15, 2017 at 4:07
  • switch $event with prop Commented Aug 15, 2017 at 4:29
  • Hi @Tùng, ng-click đó mình bắt được event rồi, đó là 1 function khác, còn func kia là getSettingHTML thì nó báo lỗi undefined, và hàm đó mình gọi bằng expression {{getSettingHTML($event)}}, không thông qua 1 action như ng-click, ng-mous... nào Commented Aug 15, 2017 at 4:43
  • 1
    @NhạHoàng theo mình biết thì cái $event nay nó chỉ có khi làm các action như click , mouse , .. thôi . Cái $event này chứa object $event của browser Commented Aug 15, 2017 at 8:01
  • $event is not just some variable that always exist. It is created when an event happens, for example the ng-click. I think you should make your question clearer; what are you trying to achieve? What do you want to do with the $event? What do you expect it to be? Commented Aug 15, 2017 at 8:02

1 Answer 1

1

Where is this $event coming from? a click on the button? If so, then the $event is going to be the first argument of an onClick event, like this:

<button ng-click="getSettingHTML($event)">click me!</button>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @jperelli, it comes from expression {{getSettingHTML($event)}}, it works fine with any action like ng-click

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.