When I tired to access an angular component value inside a custom decorator, I am getting an error stating this is not defined. From online documentation, I have found out that decorator function will get called first even before the class initialization. Because of this decorators will not have access to instance variables.
Note: I have tried creating a static variable and tried to access the static variable inside the custom decorator. Only problem with this approach is customDecorator runs first before assigning the value to static variable. Hence the AccountId is undefined when i tried to access inside customDecorator.
How I can access this AccountId inside the custom decorator? Any help would be greatly appreciated.
import { Component,Input, OnInit } from '@angular/core';
import {CustomDecorator } from '@sharedDecorators';
@Component({
selector: 'app-account-display',
templateUrl: './accounts.component.html',
styleUrls: ['./account s.component.scss']
})
export class AccountsComponent implements OnInit {
@Input() AccountId: string
constructor() {
}
@CustomDecorator(this.AccountId)
ngOnInit() {
}
...........................
}
hu Ane!
if AccountId is passed statically from parent you can simply:
ngOnInit() {
console.log(this.AccountId)
}
if AccountId is asynchronous you can do something like this
@Input() get AccountId(value: string) {
if(value) {
this.accountID$.next(value)
}
}
accountId$ = new ReplaySubject<any>()
ngOnInit() {
this.accountId$.subscribe((x) => console.log(x) ) <-- remember to unsubscribe
}
here you can find more info