Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
How to set default parameter for @Input in Angular2?

Say I have a component that will display a name property, so it roughly goes like this:

import {Component, Input} from 'angular2/core';

@Component({
  selector: 'demo',
  template: `<div>{{name}}</div>`,
  styles: [``],
})
export class Demo {
  @Input() name: string;
}

The problem is, how could I display [noname] when someone using this component but not passing any name property?

The only solution comes to mind is using logic operator inside template like {{ name || '[noname]' }}.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

try

@Input() name: string = 'noname';
over 4 years ago · Santiago Trujillo Denunciar

0

You can intercept @Input() with a setter and back it with a private field. In setter, you do a null check so that the field is set to a non-null value only. Lastly, you link your template to a private file in which you have set an initial value.

over 4 years ago · Santiago Trujillo Denunciar

0

with angular 8 and the default tslint settings your IDE will notice:

enter image description here

so it´s okay to just write:

@Input() addclass = '';

without any "type annotation".

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda