简体   繁体   中英

Angular conditionally show messages based on multiple case

I have an issue finding the best way to show a message based on multiple conditions.

I have an object like this in a file called messages.ts :

export const VALIDATION_MESSAGES = { // Around 15 messages
 message1: 'message1',
 message2: 'message2',
 message3: 'message3',
 message4: 'message4',
 ...
}

And I have a function that return me an array or required data like so :

// Example 1
[  
 'EM',
 'PL'
]

// Example 2
[  
 'AP',
 'PL',
 'EMP'
]

I have around 10 combinations, each combination is releated to a message in my messages.ts , for example ['EM', 'PL'] is related to message2, etc...

On ngOnInit I call a function to control the message to show and assign it to this.message of my component.

But I can't find the best way to do that function in a clean modern javascript/typescript way.

Thank in advance

您可以创建一个自定义管道,并将数组传递给它,并根据您的条件从transform方法返回消息

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM