【问题标题】:How can I check if the first character of a string it is a number as a condition in *ngIf如何检查字符串的第一个字符是否是数字作为 *ngIf 中的条件
【发布时间】:2021-09-29 05:59:55
【问题描述】:

我正在尝试检查它是否带有第一个字符作为数字的文档名称。我无法让我的代码进入并显示“虚拟字符串”。我尝试了这两个选项,但它不起作用。

1.
let documentPDF = "6487954.pdf"
<ng-container *ngIf="Character.isDigit(ParseInt(documentPDF.charAt(0))) == false">
         Dummy string
</ng-container>

2.
let document = "6487954.pdf"
<ng-container *ngIf="isNaN(ParseInt(documentPDF.charAt(0))) == false">
         Dummy string
</ng-container>

感谢您的帮助。

【问题讨论】:

  • 您需要在模板本身中执行所有这些逻辑吗?
  • 是的,我需要直接在模板上进行检查

标签: javascript html typescript type-conversion frontend


【解决方案1】:

你可以使用正则表达式:

试试这个:

let documentPDF = "6487954.pdf"

<ng-container *ngIf="!/^\d/.test(documentPDF)">
  Dummy string
</ng-container>
    


  

【讨论】:

  • 我收到“解析器错误:在 [!/^\d/.test(documentPDF)] 的第 2 列出现意外的令牌 /”。感谢您的尝试!
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 2011-04-03
相关资源
最近更新 更多