Hello community,
I have encountered a problem when trying to retrieve a value from custom angular component as a string. How would one tackle this task?
@Input(); @CrtInput(); token! : string; ngAfterViewInit(){ console.log(token); //token is undefined here. }
return { viewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[ { "operation": "insert", "name": "WebphoneName", "values": { "type": "test.Webphone", "token": "$token" }, "parentName": "MainContainer", "propertyName": "items", "index": 0 } ]/**SCHEMA_VIEW_CONFIG_DIFF*/, viewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[ { operation: "merge", path: ["attributes"], values: { token: { value: "default value", }, }, }, ]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/, modelConfigDiff: /**SCHEMA_MODEL_CONFIG_DIFF*/[]/**SCHEMA_MODEL_CONFIG_DIFF*/, handlers: /**SCHEMA_HANDLERS*/[ { request: "crt.HandleViewModelInitRequest", handler: async (request, next) => { request.$context.token = "21344" return next?.handle(request); } } ]/**SCHEMA_HANDLERS*/, converters: /**SCHEMA_CONVERTERS*/{}/**SCHEMA_CONVERTERS*/, validators: /**SCHEMA_VALIDATORS*/{}/**SCHEMA_VALIDATORS*/ };
Like
Hi Grzegorz,
Thanks for sharing the details!
From what I can see, the issue likely comes from trying to access the @Input() / @CrtInput() value (token) too early. At that stage, the input might not yet be initialized, especially if the component is rendered dynamically in a Freedom UI page. Try to use ngOnChanges() instead of ngAfterViewInit(). If you’ve already tried that and token is still undefined, could you please share a bit more context so we can help reproduce and debug this?
Hi Jakub,
Thanks for your answer. I have found a different solution. What do you think about it?
@Input(); @CrtInput(); token! : string; ngAfterViewInit(){ setTimeout(() => console.log(token), 1); }
Hi Grzegorz, setting a timeout might not be reliable in all cases. You can try the ngOnChcnges or add validation for example if(!token) to check if it's already populated
Yes, ngOnChanges might be better in most cases as it also will listen to changes from the inputs, so if your inputs are retrieved from the creatio sys, the changes and the logic will be automatically re-invoked. As for If(!token) you would also need to think about handling, what if the input will not be populated.