Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
¿Cómo envío el contenido secundario desde un componente reutilizable usando @Input?

Usando Angular 12.

Quiero crear un componente de botón reutilizable. Quiero obtener el texto dentro de las etiquetas donde alojo mi componente y luego obtenerlo como una entrada dentro de mi componente.

Dentro de mi home.component.html:

 <app-ui-text-button color="#fe7f2d">Press Me!</app-ui-text-button>

¡Quiero obtener Presioname! texto y enviarlo como entrada a mi componente.

Mi componente:

 @Component({ selector: 'app-ui-text-button', styles: [` .button { padding: 12px 8px; border-radius: 5px; cursor: pointer; } .button:hover { filter: brightness(1.2); } `], template: ` <div class="button" [style.backgroundColor]="color" (click)="onClick($event)">{{ text }}</div> ` } ) export class AppUiTextButtonComponent { // Inputs @Input() text = 'sdfs'; @Input() color = '#619b8a'; // @Input() childContent: ElementRef<any> = new ElementRef(any); // Outputs @Output() clicked = new EventEmitter<any>(); onClick(event: Event) { this.clicked.emit(event); } }

Entonces quiero tomar el texto e insertarlo como variable dinámica dentro de mi div.

Yo he tratado

 @Input() childContent: ElementRef<any> = new ElementRef(any); @Input() text = '';

sin exito. También traté de buscar en línea, pero no obtuve respuestas claras.

Gracias de antemano.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Método 1: método sencillo

si desea el texto en su componente personalizado. puedes hacerlo como

 <app-ui-text-button color="#fe7f2d" [text]="Press Me!"></app-ui-text-button>

luego el text @Input tendrá Press Me! en tu componente!

Método 2: usando la proyección de contenido

ui-text-button.component.ts

 @Component({ selector: 'app-ui-text-button', styles: [` .button { padding: 12px 8px; border-radius: 5px; cursor: pointer; } .button:hover { filter: brightness(1.2); } `], template: ` <div class="button" [style.backgroundColor]="color" (click)="onClick($event)"> <ng-content></ng-content> <!-- here added this ng-content tag --> </div> ` } ) export class AppUiTextButtonComponent { // Inputs // @Input() text = 'sdfs'; // don't need this anymore with content projection @Input() color = '#619b8a'; // @Input() childContent: ElementRef<any> = new ElementRef(any); // Outputs @Output() clicked = new EventEmitter<any>(); onClick(event: Event) { this.clicked.emit(event); } }

ahora usando este ui-text-button como quieras usar en tu ejemplo

 <app-ui-text-button color="#fe7f2d">Press Me!</app-ui-text-button>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!