最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 Pipe for changing inline style of specific character - Stack Overflow

programmeradmin1浏览0评论

I am trying to make an Angular2 pipe that will filter through a line of text and see if a the "#" sybmol is in the text. If it is, then I would like the color to change to red but only '#' should change to red, not the entire string. Below is what I have thus far.

@Pipe({ name: 'redStar' })
export class RedStarPipe implements PipeTransform{
    transform(text: string, numLetters: number){
        if(text.includes("*")) {
            if(numLetters === undefined) {
                var str = text.replace('#', '<span style="color: red">*</span>');
                return str;
            }
        } else {
            return text;
        }
    }
}

I am trying to make an Angular2 pipe that will filter through a line of text and see if a the "#" sybmol is in the text. If it is, then I would like the color to change to red but only '#' should change to red, not the entire string. Below is what I have thus far.

@Pipe({ name: 'redStar' })
export class RedStarPipe implements PipeTransform{
    transform(text: string, numLetters: number){
        if(text.includes("*")) {
            if(numLetters === undefined) {
                var str = text.replace('#', '<span style="color: red">*</span>');
                return str;
            }
        } else {
            return text;
        }
    }
}
Share Improve this question edited Oct 26, 2017 at 13:31 Martin Adámek 18.5k5 gold badges48 silver badges71 bronze badges asked Oct 26, 2017 at 13:16 DevilRunnerDevilRunner 511 silver badge7 bronze badges 2
  • What issue are you facing? – Hacker Commented Oct 26, 2017 at 13:35
  • This is the edited code: transform(text: string){ if(text.includes("#")){ var str = text.replace('#', 'TEST'); return str; } else{ return text; } } The code is not even replacing the # characters with "TEST" – DevilRunner Commented Oct 26, 2017 at 13:50
Add a ment  | 

2 Answers 2

Reset to default 2

The problem with your code is that you want to replace # hash character, but in your pipe you are checking the * asterisk character.

Just change if (text.includes("*")) { to if (text.includes("#")) { and it will be working.

Also to allow replacing multiple # characters, you need to use regex with g modifier:

text.replace(/#/g, '<span style="color: red">*</span>')

Here is a working example:

https://stackblitz./edit/angular-playground-djvdwb

here is a fiddle with the soltution https://stackblitz./edit/angular-5am1ni pipe.ts

import { Component, Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'
@Pipe({ name: 'redStar' })
export class RedStarPipe implements PipeTransform{
constructor(private sanitized: DomSanitizer) {}
transform(text: string, numLetters: number){
let text1;
if(text.indexOf("#") !== -1){  
    var str = text.replace(/#/g, "<span style='color: red'>*</span>");
    text1 = str;
}else{
   text1 = text;
 }
 return this.sanitized.bypassSecurityTrustHtml(text1);;
 }}

ponent.html

<div [innerHTML]="name  | redStar"></div>
发布评论

评论列表(0)

  1. 暂无评论