我有一个 ngFor 循环,它循环一个字符串数组 sideBarRoutesPath
。
<div class="sidebar-feature" *ngFor = "let side of sideBarRoutesPath">
<button mat-button color="primary" class="feature-button">
{{side}}
</button>
</div>
我想做的事情:
现在,我的 {{side}}
正确显示字符串。但是,字符串中包含 '/'
,但我不希望在 HTML 上显示 '/'
符号。
["/devices/", "/account/overview"]
我不想删除字符串数组本身中的 '/'
,因为我需要 '/'
来实现路由目的。所以我想我必须创建第二个数组来复制我的第一个数组,然后删除 /
或使用正则表达式在 *ngFor
中执行此操作。
我尝试过: sideBarRoutesPath.replace(/\W/g, '')
它消除了 '/' 但随后它将所有字符串混合在一起,所以这不起作用。
最佳答案
您可以在组件类中定义一个方法来删除 /
字符:
public removeSlashes(side: string): string {
return side.replace(/\//g, "");
}
并在模板中调用它:
{{ removeSlashes(side) }}
关于javascript - Angular: *ng对于字符串数组,删除 '/' 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48799057/