export class ClassName implements OnInit {
url: string = "{{'content.url' | translate}}";
urlSafe: SafeResourceUrl;
constructor(public sanitizer: DomSanitizer, private translate: TranslateService) { }
ngOnInit() {
this.translate.stream('content.url').subscribe((text: string) => {
this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(text);
console.log(this.urlSafe);//logs the SafeResourceUrl Object
this.runSpecificFunction();
});
}
runSpecificFunction() {
console.log(this.urlSafe); //undefined
}
}
Why is it that in the ngOnInit
method of this class, I can log the SafeResouorceUrl
object successfully?
However, I am encountering difficulties accessing the URL from that object in the runSpecificFunction
method below.
Although the code within runSpecificFunction
could be executed within ngOnInit
, it needs to be structured this way for a specific purpose. Unfortunately, I cannot understand why I am getting undefined.
Can someone please point out what I might be doing wrong in this Angular class? As a new learner, I appreciate any guidance and support provided!