I added an onblur
event to all input fields, and I want to get the $scope
and $element
when the onblur
event runs. But when I use this.$element
to get the element, I get undefined
.
When I log only this, I get the input tag.
How can I get the $scope
and $element
on an onblur
event?
Code:
constructor($document, $element, $scope) {
this.$document = $document;
this.$element = $element;
this.$scope= $scope;
}
FunCall() {
const formElements = this.$element[0].querySelectorAll('input');
const watchElement = angular.element(formElements[0]);
console.log(this);
watchElement[0].onblur = function() {
console.log(this); // html input element
console.log(this.$scope); // undefined
console.log(this.$element); // undefined
};
}
On the first log I get the correct this, which is the controller. On the second log, inside the onblur
event I get the HTML element and get undefined
for the $scope
and $element
. How can I get the controller, like the first one inside the onblur
event?
And I want to run a function inside the onblur
event, but this didn't work:
watchElement[0].onblur = function() {
this.runSecondFunction();
};
runSecondFunction() {
console.log('test 2nd function');
}