In my TypeScript Angular controller, I am looking to create a method that triggers every time the URL changes - even if it's just a small part of the URL like a number. I have already implemented the method I want to trigger and it is located within the constructor of the controller. My question is how do I determine when to trigger this method and where should I call it? Should I initialize it inside the constructor?
Here is an excerpt of my controller's code:
private returnedClass: Class;
static $inject: Array<string> = ['$scope', 'common', 'repository.class'];
constructor(public scope: any, public common: app.core.Common, public myService: app.data.IRepositoryClass) {
}
getFromDB(): void{
if (location.href.indexOf("classAdd") !== -1 && typeof this.common.itemId !== 'undefined' && this.common.itemId != null)
{
this.myService.getClassById(this.common.itemId).then((returnedItem: Class) => {
this.returnedClass = returnedItem;
console.log(this.returnedClass);
});
}
}
}
angular
.module('app.layout')
.controller('HeaderNavigationController', HeaderNavigationController);
I should note that this controller is executed once when the entire HTML DOM is loaded (index.html). Do I need to implement a watcher or some other mechanism for this? How can I achieve that?
Thank you in advance