0

I've created a function similar to many others I've done before, where I've passed in $event via the controller where it's placed, and also in the element's function call in the HTML for an ng-change event, but the console is saying that $event is undefined in all browsers.

HTML:

<select data-ng-change="determineAbsenceInputs($event)"

JS:

$scope.determineAbsenceInputs = function($event) {
    console.log($event.target);
}

Also returning the same in JSFiddle: http://jsfiddle.net/ADukg/16190/.

Written like every other function I've made where I've used it to reference the event target and it's attributes, but cannot figure out why this is happening only for this function.

1 Answer 1

1

From issue

ng-change is not a directive for handling the change event (I realize that this is confusing given the name), but is actually instead notified when ngModelController.$setViewValue() is called and the value changes (because ng-change adds a listener to the $viewChangeListeners collection). So this is as expected.

You should use ng-model instead

  <select data-ng-change="determineAbsenceInputs(selectedAbsenceType)" data-ng-model="selectedAbsenceType" id="absenceTypeSelect" name="absenceTypeSelect" material-select watch class="validate" required>

WORKING DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, thanks. Didn't know that. How in that case would this approach be used to access the data-absenceclass of the selected element then if event.target cannot be used?
you should use ng-value in that case. mark if this helped

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.