Javascript 使用Substring和ngIf查看字符串是否在另一个字符串中
我正在做以下工作,试图检查addressDescription是否包含单词Maple street。 如果收到以下错误,将如何修复Javascript 使用Substring和ngIf查看字符串是否在另一个字符串中,javascript,html,css,angular,angular8,Javascript,Html,Css,Angular,Angular8,我正在做以下工作,试图检查addressDescription是否包含单词Maple street。 如果收到以下错误,将如何修复 <app-address-mailing *ngIf="message.addressDescription.indexOf('Maple') > -1"></app-address-mailing> 错误提示: 消息。地址描述未定义 因此,您可以添加一个新条件: *ngIf="message?.addressDescription
<app-address-mailing *ngIf="message.addressDescription.indexOf('Maple') > -1"></app-address-mailing>
错误提示:
消息。地址描述
未定义
因此,您可以添加一个新条件:
*ngIf="message?.addressDescription && message.addressDescription.indexOf('Maple') > -1"
使用此条件,您将确保其有效。消息未定义,请先选中,然后使用包含
如果addressDescription
是一个句子,请尝试以下操作
<app-address-mailing *ngIf="message?.addressDescription.split(' ').includes('Maple')"></app-address-mailing>
<app-address-mailing *ngIf="message?.addressDescription.includes('Maple')"></app-address-mailing>
否则,请尝试以下操作
<app-address-mailing *ngIf="message?.addressDescription.split(' ').includes('Maple')"></app-address-mailing>
<app-address-mailing *ngIf="message?.addressDescription.includes('Maple')"></app-address-mailing>
不用说:您可以将应用于小写方法。试试*ngIf=“message.addressDescription?.indexOf('Maple'))
使用可为空的运算符hi@terahertz hmm,可以避免编译错误,现在它呈现html项,即使Maple不存在,我只是尝试了一下,并且没有真正检查if语句就显示出来,奇怪