简体   繁体   English

是否可以在NgClass指令中使用正则表达式?

[英]Is it possible to use Regex Expressions in the NgClass Directive?

This is the row section of my Material Table, I am able to highlight the specified row if I put the exact value after row.name, but what I am trying to do is highlight every row that has the name Rage in it not caring about any characters afterwards, naturally I tried to apply regex expressions. 这是我的材料表的行部分,如果将​​确切的值放在row.name之后,则可以突出显示指定的行,但是我想做的是突出显示名称中带有Rage的每一行之后的任何字符,自然地我尝试应用正则表达式。

I have tried to use the ngPattern directive but I could not figure any workable solution 我尝试使用ngPattern指令,但找不到任何可行的解决方案

   <mat-row  
       *matRowDef="let row; columns: displayedColumns"
        [ngClass]="{'highlight': selectedRowIndex == row.id, 
                    'move_highlight': row.name === 'Rage.*'}">
   </mat-row>

I was expecting that after I put the .* after Rage that it would detect every row based off the regex expression but instead it did nothing. 我期望在将。*放到Rage之后,它会根据正则表达式表达检测到每一行,但是却什么也没做。

That's just a string comparison as you have written it, testing if row.name literally equals 'Rage.*' . 这只是您编写时的字符串比较,测试row.name确实等于'Rage.*'

What you really want is: 您真正想要的是:

/^Rage.*/.test(row.name)

I'm not sure you can actually drop a regex into Angular HTML like that, however, so what I'd probably do is create a function in your TypeScript class file like this: 我不确定您是否真的可以像这样将正则表达式放入Angular HTML中,所以我可能要做的就是在TypeScript类文件中创建一个函数,如下所示:

  testForRage(s: string) {
    return /^Rage.*/.test(s);
  }

And then put: 然后放:

'move_highlight': testForRage(row.name)}">

...into your HTML. ...转换成您的HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM