I've got an interface (option.ts):
export interface Option {
label: string;
model: any;
}
Which I then import into my component (form-select.component.ts):
import {Component, Input, Output, EventEmitter} from 'angular2/core';
import {Option} from './option';
@Component({
selector: 'form-select',
templateUrl: './app/assets/scripts/modules/form-controls/form-select/form-select.component.html',
styleUrls: ['./app/assets/scripts/modules/form-controls/form-select/form-select.component.css'],
inputs: [
'options',
'callback',
'model',
'label'
]
})
export class FormSelectComponent {
@Input() model: any;
@Output() modelChange: EventEmitter = new EventEmitter();
isOpen: boolean = false;
toggle() {
this.isOpen = !this.isOpen;
}
close() {
this.isOpen = false;
}
select(option, callback) {
this.model = option.model;
this.modelChange.next(this.model);
callback ? callback() : false;
}
}
Now I would like to use this interface to ensure that [options]
that is passed to my select component from another component is correctly formatted and have the right types. [options]
must be an array looking like this:
options = [
{label: 'Hello world', model: 1},
{label: 'Hello universe', model: 2},
{label: 'Hello honey boo boo', model: 3}
]
Where each index object will use the Option
interface, but how do I do this? I remember something along the lines of using Option[]
in your component class but I'm not sure how to use [options]
along with the interface and then passing it to the view (or however you're supposed to do it).
How can I achieve this?