0

Can I declare a input var in a directive as required or even better as non-required? We have default value set to false here in example, but if I forget to declare in parent component template, then ng2 AoT says its an error:

 @Input() serverWaiting: boolean = false; 

1 Answer 1

2

@Input are non-required as default. I don't think there is an option to make them required but you can do something like this:

@Input() serverWaiting: boolean = false; 
@Input() variableRequired: string; 
ngOnInit(){
    if(this.variableRequired==null){
       throw new Error('variableRequired field is required')
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.