Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Substring和ngIf查看字符串是否在另一个字符串中_Javascript_Html_Css_Angular_Angular8 - Fatal编程技术网

Javascript 使用Substring和ngIf查看字符串是否在另一个字符串中

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

我正在做以下工作,试图检查addressDescription是否包含单词Maple street。 如果收到以下错误,将如何修复

<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语句就显示出来,奇怪