简体   繁体   English

如何在常规 Javascript 中使用 Angular pipe 循环?

[英]How to use Angular pipe in regular Javascript for loop?

I'm using a highlight pipe in Angular's appComponent to highlight search text displayed in the router-outlet from another component but I don't know how to use the pipe in a component (not in a template).我在 Angular 的 appComponent 中使用突出显示 pipe 来突出显示在另一个组件的router-outlet中显示的搜索文本,但我不知道如何在组件中使用 pipe(不在模板中)。

What I currently have in app.component.ts :我目前在app.component.ts中拥有的内容:

import { Component, OnInit } from '@angular/core';
import { HighlightPipe } from './shared/highlight-pipe';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  providers: [ HighlightPipe ]
})

export class AppComponent {
  constructor(private highlight: HighlightPipe) {}

  startSearch(searchTerm: string) {
    if (!searchTerm) {
      return;
    }
    const content = document.getElementById('content');
    if (content) {
      const mainContent = content.children;

      for (let i = 0; i < content.length; i++) {
        **content[i].innerHTML = 'content[i].innerHTML | this.highlight: searchTerm';** <-- here
      }
    }
  }
}

My app.component.html :我的app.component.html

<app-header (searchEvent)="startSearch($event)"></app-header>
<router-outlet></router-outlet>

My highlight-pipe.ts :我的highlight-pipe.ts

import {PipeTransform, Pipe} from '@angular/core';

@Pipe({ name: 'highlight' })
export class HighlightPipe implements PipeTransform {
  transform(text: string, search): string {
    let pattern = search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
    pattern = pattern.split(' ').filter((t) => {
      return t.length > 0;
    }).join('|');
    const regex = new RegExp(pattern, 'gi');

    return search ? text.replace(regex, (match) => `<span class="highlight">${match}</span>`) : text;
  }
}

So (predictably) my output currently just repeatedly prints the string content[i].innerHTML | this.highlight: searchTerm所以(可以预见)我的 output 目前只是重复打印字符串content[i].innerHTML | this.highlight: searchTerm content[i].innerHTML | this.highlight: searchTerm in the <router-outlet> content[i].innerHTML | this.highlight: searchTerm <router-outlet>中的 searchTerm

I've seen other articles that talk about using *ngFor but *ngFor is used in a template.我看过其他文章谈论使用 *ngFor 但 *ngFor 在模板中使用。 But in my case, when someone tries to do a search in the header (not App) component, the header component simply emits the searchEvent event that is then received in the AppComponent template, which triggers startSearch in AppComponent ... so I have to use the pipe in AppComponent instead of in the header.但在我的情况下,当有人尝试在 header(不是 App)组件中进行搜索时,header 组件只是发出searchEvent事件,然后在AppComponent模板中收到该事件,触发startSearch中的AppComponent ...所以我必须在 AppComponent 中使用AppComponent而不是在 header 中。

Is it possible to use a pipe in a plain JS for loop in a component instead of in a template in Angular?是否可以在组件中的普通 JS for循环中使用 pipe,而不是在 Angular 的模板中使用?

EDIT:编辑:

With the pipe added to your constructor:将 pipe 添加到您的构造函数中:

constructor(private highlight: HighlightPipe) {} 

Then you can use:然后你可以使用:

content[i].innerHTML = this.highlight(content[i].innerHTML, "searchTerm")

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

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