Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]' }}.

like image 601
jasonslyvia Avatar asked Mar 07 '16 08:03

jasonslyvia


1 Answers

try

@Input() name: string = 'noname'; 
like image 175
Качалов Тимофей Avatar answered Sep 30 '22 08:09

Качалов Тимофей