I'm facing an issue with my code. In Ionic with Angular, I usually access Angular services in the constructor after injecting them like this:
export class HomeController {
static $inject = [
'$scope',
'$state'
];
constructor(
public $scope : any,
public $state : any
) {
this.$state.go('test');
this.$scope.changeController = this.changeController;
}
changeController() {
console.log("change controller");
};
}
However, when I modify it to the following structure, it stops working:
export class HomeController {
static $inject = [
'$scope',
'$state'
];
constructor(
public $scope : any,
public $state : any
) {
this.$scope.changeController = this.changeController;
}
changeController() {
console.log("change controller");
this.$state.go('test'); // This line fails
};
}
An error is thrown:
Error: undefined is not an object (evaluating 'this.$state.go')
Any suggestions on how to resolve this?
Additionally, is it appropriate to add the changeController function to the scope, or is there a simpler way to make it accessible to a template?
Thank you in advance