Pascal
Pascal

Reputation: 12885

$event passed with ng-click is undefined

Why is the passed $event object in the toggleDropdown function undefined?

This is the original code from: http://plnkr.co/edit/koNsQycAAiEmI8jBApS2?p=preview

except that the original code does not have the toggleDropdown function in the button-tag defined. I did this because the original code does NOT open the dropdown on my computer.

I use angularjs 1.3.2 and angularjs-bootstrap 0.11.2

'use strict';
angular.module('auth').controller('AccountController', function ($scope) {

  $scope.status = {
    isopen: false
  };

  $scope.toggleDropdown = function($event) {
    $event.preventDefault();
    $event.stopPropagation();
    $scope.status.isopen = !$scope.status.isopen;
  };

});

<!-- Single button -->
<div class="btn-group" dropdown is-open="status.isopen">
  <button ng-click="toggleDropdown()" type="button" class="btn btn-primary dropdown-toggle" dropdown-toggle ng-disabled="disabled">
    Button dropdown <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">
    <li><a href="#">Action</a></li>
    <li><a href="#">Another action</a></li>
    <li><a href="#">Something else here</a></li>
    <li class="divider"></li>
    <li><a href="#">Separated link</a></li>
  </ul>
</div>

Upvotes: 15

Views: 14013

Answers (2)

rayners
rayners

Reputation: 539

Looks to me like you need to have jQuery loaded as well to get a meaningful event object. See my fork of your plunk: http://plnkr.co/edit/ZpQuz1?p=preview

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

Upvotes: -2

New Dev
New Dev

Reputation: 49590

You have to explicitly specify that you are passing the $event variable:

<button ng-click="toggleDropdown($event)">

Upvotes: 13

Related Questions