英文:
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 '@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>`);
}
In your 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>
In your .css file
::ng-deep .selected {
color:red;
}
Edit : I've put together a stackblitz in case of any hurdles
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论