I recently started using ES6 class for defining my controller, and here is the syntax I am using:
export class SearchBarController {
constructor($log) {
'ngInject';
$log.debug("Hello");
}
textTyped($log){
$log.debug("change fired.");
}
}
In the view :
<input type="text" data-ng-model="vm.txt" data-ng-change="vm.textTyped()"/>
The issue I am facing is that although "Hello" from within the constructor gets logged successfully, "change fired" within the typedText() function does not fire because it is apparently undefined. How can I ensure that my class function textTyped() has access to the $log service?
Please Note: If I assign $log to a class property in the constructor like this,
this.logger = $log;
And then use it like this,
this.logger.debug("Change fired.");
it works. However, I am unsure if this is the correct approach.
Update: Additionally, this approach exposes the this reference to the $log service in the view associated with this controller. Is this potentially harmful?
Is there a more optimal solution?