Imagine you have a custom component that is designed to show a name
property. The code for this component might look something like this:
import {Component, Input} from 'angular2/core';
@Component({
selector: 'demo',
template: `<div>{{name}}</div>`,
styles: [``],
})
export class Demo {
@Input() name: string;
}
An issue arises when someone uses this component without passing a value for the name
property. How can we make it display [noname]
in such cases?
The simplest solution that comes to mind is using a logic operator within the template, like so: {{ name || '[noname]' }}
.