Home > OS >  typescript private variables message
typescript private variables message

Time:11-30

In my typescript class I have two private variables. One variable is goping to store a string that I am returning from an API. The other one is a message that I will be displaying to the user. Once the API is completed and will use the return and build the full message. In the response I am setting this.templatIds so it will build the message with the correct ids that. But in my popup message its missing the section from the string.

export class AgentsGridComponent implements AfterViewInit {
    private templatIds = "";
    private inuseItemMessage = `This Agent is being used by Templates ${this.templatIds} and can't be archived`




this.agentsService.inuseTemplateAgentList(data.item.Id)
                    .then((response) => {
                        console.log(response.Value);
                        this.templatIds = response.Value;
                        this.notificationManager.error(inuseItemMessage);
                    });

CodePudding user response:

The templatIds is not populated at the time that the inuseItemMessage is assigned to - they both get assigned at the same time, at the beginning of the constructor. Make the message into a function instead, so it can construct the template string from the now-populated templatIds on demand.

private getInUseMessage = () => `This Agent is being used by Templates ${this.templatIds} and can't be archived`
this.notificationManager.error(this.getInUseMessage());
  • Related