清除*ngfor中的字符串将删除不匹配的项

mccptt67  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(193)

我有一个简单的*ngfor循环,显示标题列表。

fetchData = [{"title":"woman%20.gif"},{"title":"aman",},{"title":"jessica",},{"title":"rosh"}];

<div *ngFor = "let title of fetchData">
  {{sanitiseIconString(title.title)}}
</div>

我通过我的消毒剂方法运行它来移除所有 %20 参考资料和任何东西,包括 .extension 例如 .gif , .jpg , .png ```
sanitiseIconString(str: string): string {
return str && str.substring(0, str.lastIndexOf(".")).replace(/%20/g, " ");
}

我的循环只返回 `woman` 哪些已经正确消毒,但为什么其他的没有显示?
https://stackblitz.com/edit/ngfor-example-2atjev?file=app/app.component.ts
cngwdvgl

cngwdvgl1#

更新app.component.ts文件中的sanitiseiconstring
这是因为你的另一个头衔没有。20%,因此条件始终为false且不返回

sanitiseIconString(str: string): string {
    return str.substring(0, str.lastIndexOf(".")).replace(/%20/g, " ")? str.substring(0, str.lastIndexOf(".")).replace(/%20/g, " "):str;
    }
d7v8vwbk

d7v8vwbk2#

如果要删除%20,请使用 string.replace() ,这将更容易和简单

<div *ngFor="let title of fetchData">
      {{title.title.replace('%20','')}}
      <!-- {{sanitiseIconString(title.title)}} -->
    </div>

相关问题