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

297
Views
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 answers
Answer question

0

try

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

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 Report

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 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!