In AngularJs we could make a directive attribute required. How do we do that in Angular with @Input? The docs don't mention it.
Eg.
@Component({
selector: 'my-dir',
template: '<div></div>'
})
export class MyComponent {
@Input() a: number; // Make this a required attribute. Throw an exception if it doesn't exist.
@Input() b: number;
}
Check in
ngOnInit()
(inputs aren't yet set when the constructor is executed) whether the attribute has a value.You might also check in
ngOnChanges(changes) {...}
if the values wasn't set tonull
. See also https://angular.io/docs/ts/latest/api/core/OnChanges-interface.html