Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
Javascript string with HTML

I have a component that looks something like this:

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  expandText: boolean = false;

  constructor() {}

  ngOnInit() {}

  stringShortener(text: string) {
    let str = text;

    if (this.expandText === false) {
      if (str.length > 80) {
        str =
          str.substring(0, 80 - 3) +
          '...' +
          ' ' +
          "<span style='color: red'>see more</span>";
      }
    } else {
      str = str + '...' + ' ' + "<span style='color: yellow'>see more</span>";
    }

    return str;
  }
}

On the HTML side it looks something like:

<div class="wrapper" (click)="expandText = !expandText">
<p>{{ stringShortener("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum") }}</p>
</div>

The issue is, that the HTML of myhtml is not rendered. I have tried with innerHTML, and that resulted in a span in some way. But if the span is something like: <span style="color: red">, or even if I use [ngStyle], [style.color], well, the style is not applied. So I only get a span with the text inside.

So how is this done with the proper rendering ?

EDIT: I have created a Stackblitz that shows the problem: https://stackblitz.com/edit/angular-ivy-xdi3z5?file=src/app/app.component.html

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The issue is that you're passing your block with span tag as a text, so Angular just prints it as a normal text.

You can achieve desired functionality with *ngIf directive which will show needed span block, by adding a function which will tell you whether text is too long.

about 4 years ago · Juan Pablo Isaza Relatório

0

Looks like you forgot the curly braces :)

    <div (click)="expandText= !expandText">
     <p>{{stringShorterner(myText)}}></p>
    </div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda