您好我尝试实现过滤搜索词应该突出显示文本,所以我使用第一个指令,现在切换到管道在IE11浏览器下面测试是代码。但是下面的代码工作正常和firefox我不确定为什么在IE11中出现这个错误。使用angular2 2.2.3
帮助一些人来解决这个错误highlight.pipe.ts:
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({ name: 'highlight' })
export class HighlightPipe implements PipeTransform {
transform(text: string, search): string {
if (search && text) {
let pattern = search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
pattern = pattern.split(' ').filter((t) => {
return t.length > 0;
}).join('|');
const regex = new RegExp(pattern, 'gi');
return text.replace(regex, (match) => `<span class="search-highlighterm">${match}</span>`);
} else {
return text;
}
}
}
成分:
@Component({
selector: 'xxx',
template:
`
<span class="title" [innerHTML]="text | highlight: searchTerm">{{text}}'
)
或者如果我使用如下指令
<span class="title" [highlight]="search" >{text}}
我收到如下错误
- inline template:6:102 caused by: Invalid argument.
ORIGINAL EXCEPTION: Invalid argument.
ORIGINAL STACKTRACE:
Error: Invalid argument.
at DomRenderer.prototype.setText (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:42348:67)
at DebugDomRenderer.prototype.setText (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:71926:72)
at View_xxxxx1.prototype.detectChangesInternal (Function code:326:5)
at AppView.prototype.detectChanges (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:73431:9)
at DebugAppView.prototype.detectChanges (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:73524:13)
at ViewContainer.prototype.detectChangesInNestedViews (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:73616:17)
at View_xxxxx0.prototype.detectChangesInternal (Function code:114:3)
at AppView.prototype.detectChanges (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:73431:9)
at DebugAppView.prototype.detectChanges (https://localhost:8080/vvv/js/webpack-bundles/vendor.bundle.js:73524:13)
答案 0 :(得分:0)
由于我无法看到您尝试使用Directive,这是我的解决方案。我不能在IE上测试它因为我和Ubuntu一起。
@Directive({
selector: '[highlight]'
})
class WrapBold implements OnInit {
@Input('highlight')
public set search (val: string) {
this._search = val;
this.highlightText();
}
public get search () : string {
return this._search;
}
private originEl: HTMLElement;
constructor(private el: ElementRef){
}
ngOnInit() {
this.originEl = this.el.nativeElement.cloneNode(true);
this.highlightText();
}
private highlightText () {
if (this.search && this.originEl) {
let pattern = this.search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
pattern = pattern.split(' ').filter((t) => {
return t.length > 0;
}).join('|');
const regex = new RegExp(pattern, 'gi');
console.log(this.originEl);
return this.el.nativeElement.innerHTML = this.originEl.innerHTML.replace(regex, (match) => `<strong>${match}</strong>`);
}
}
}
组件
@Component({
selector: 'my-app',
template: `
<div [highlight]="search">
Hello Angular2
</div>
`
})
export class App {
search = 'angu';
constructor() {
// change the search string after 3sec
setTimeout(() => { this.search = 'angular'; }, 3000)
}
}