在Angular中是否可以突出显示文本?

huangapple go评论70阅读模式
英文:

Is it possible to highlight text in angular?

问题

我正在尝试在{{receipt}}输出中突出显示提交的搜索查询。

component.html:

          <h5 style="text-decoration: underline">您的收据</h5>
          <pre>
            {{receipt}}
          </pre>
          产品搜索:<input type="text" (input)="onSearchChange($event.target.value)">
          <br>
          <span>总价格:{{total}}</span>
          </div>

component.ts:

  onSearchChange(SearchValue){
    if((this.receipt).includes(SearchValue)){
    };

如您所见,到目前为止,我唯一成功完成的事情是检查提交的输入是否存在于receipt变量中。我想要以某种方式突出显示它。

任何建议将不胜感激。谢谢

编辑:我正在查看:
https://stackblitz.com/edit/angular-highlight-directive

但我不确定如何在我的情况下实现该指令。

英文:

I am trying to highlight the search query submitted into the input in the {{receipt}} output.

component.html:

          <h5 style="text-decoration: underline">Your Receipt</h5>
          <pre>
            {{receipt}}
          </pre>
          Product Search:<input type="text" (input)="onSearchChange($event.target.value)">
          <br>
          <span>Total Price: {{total}}</span>
          </div>

component.ts:

  onSearchChange(SearchValue){
    if((this.receipt).includes(SearchValue)){
    };

As you can see, the only thing I managed to accomplish so far is to check whether or not the input which was submitted exists in the receipt variable. I would like to somehow highlight it.

Any suggestions would be appreciated. Thank you

Edit: I was looking at:
https://stackblitz.com/edit/angular-highlight-directive

But I am not sure how to implement that directive in my case

答案1

得分: 4

没有指令,您可以使用以下代码实现相同的功能:

在您的TS文件中

import { Renderer2} from '@angular/core';

@ViewChild('refEl', { static:true }) refEl;

constructor(private renderer: Renderer2) {  }

receipt = "hello"

onSearchChange(value){
   if((this.receipt).includes(value)){
     this.renderer.setProperty(
       this.refEl.nativeElement,
       'innerHTML',
       this.getFormattedText(value)
     );       
   };
}

getFormattedText(value){
   const valuestring = value.trim().split(' ')
   const re = new RegExp(`(${ valuestring.join('|') })`, 'g');
   return this.receipt.replace(re, '<span class="selected">$1</span>');
}

在您的HTML文件中

<div>
   <h5 style="text-decoration: underline">Your Receipt</h5>
   <pre #refEl> {{receipt}} </pre>
   Product Search:<input type="text" (input)="onSearchChange($event.target.value)">
   <br>
   <span>Total Price: {{total}}</span>
</div>

在您的.css文件中

::ng-deep .selected {
   color:red;
}

编辑:我已经在stackblitz上创建了一个示例以解决任何问题。

英文:

Without directive you can implement the same using the following code

In your TS file

 import { Renderer2} from &#39;@angular/core&#39;;

 @ViewChild(&#39;refEl&#39;, { static:true }) refEl;
 
 constructor(private renderer: Renderer2) {  }

 receipt = &quot;hello&quot;

 onSearchChange(value){
   if((this.receipt).includes(value)){
     this.renderer.setProperty(
     this.refEl.nativeElement,
      &#39;innerHTML&#39;,
      this.getFormattedText(value)
    );       
  };
 }

 getFormattedText(value){
    const valuestring = value.trim().split(&#39; &#39;)
    const re = new RegExp(`(${ valuestring.join(&#39;|&#39;) })`, &#39;g&#39;);
    return this.receipt.replace(re, `&lt;span class=&quot;selected&quot;&gt;$1&lt;/span&gt;`);
 }

In your html

 &lt;div&gt;
   &lt;h5 style=&quot;text-decoration: underline&quot;&gt;Your Receipt&lt;/h5&gt;
   &lt;pre #refEl&gt; {{receipt}} &lt;/pre&gt;
    Product Search:&lt;input type=&quot;text&quot; (input)=&quot;onSearchChange($event.target.value)&quot;&gt;
   &lt;br&gt;
   &lt;span&gt;Total Price: {{total}}&lt;/span&gt;
 &lt;/div&gt;

In your .css file

 ::ng-deep .selected {
   color:red;
  }

Edit : I've put together a stackblitz in case of any hurdles

huangapple
  • 本文由 发表于 2020年1月6日 20:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定