I am trying to fill an array with custom objects. The icon property is optional and when it's not provided it must use a default value.
class type:
export class Shortcut {
text: string;
icon?: string = 'fa-external-link';
}
typescript dashoard component
export class DashboardComponent implements OnInit {
shortcutList: Shortcut[] = [];
ngOnInit() {
this.getShortcuts();
}
getShortcuts(): void {
this.shortcutList = [
{ text: 'shortcut 001'}
{ text: 'shortcut 002', icon: 'fa-users' }
];
}
}
The default icon in the first record is missing. I tried it with a constructor but I didn't work for me.
Shortcutis a class that you never instanciate in yourshortcutList. It defaults to behave like an interface (that cannot have default field value).