首页 > 解决方案 > 动态地将类添加到字符串中的单词Angular

问题描述

如果匹配某些条件,我想为字符串中的单词添加一个类

Your Bulk Creation: 8312e9, has Completed With Error. Please navigate to Outgoing Invoices> Create> Bulk History to view results. 

我有这个字符串,现在如果字符串包含状态,CompletedWithError那么在上面的文本中我必须在单词中添加一个类Completed With Error

元数据如下:

dateCreated: "2021-08-06T06:26:16.621Z"
 isError: false
 isRead: false
 message: "Your Bulk Creation: 8312e9, has Completed With Error. Please navigate to Outgoing Invoices> Create> Bulk History to view results."
 metaData: {
    recordType: "Invoice", 
    recordId: "610942b92c463500128312e9", 
    recordStatus: "CompletedWithError"
 }

该字符串是动态的,也可以包含任何其他状态,有什么办法吗?

标签: javascriptangularng-class

解决方案


您可以检查这些字符串是否存在,如果它们确实存在,然后将其作为布尔值返回,如果其为真,然后将其添加到消息中,您可以重新制定响应对象 - 我尽可能简单地保留它。包含区分大小写。

 if(message){
    const contains = message.includes('Completed With Error')
    
    if(contains){
  return  message + 'Has Completed With Errors'
    }

}

推荐阅读