Last active
March 8, 2018 19:57
-
-
Save azanebrain/235434719a324bada0cf538763f21df1 to your computer and use it in GitHub Desktop.
Strongly typed Angular form interfaces ~ http://azanebrain.github.io/news/angular-snafoo-testing-component-with-overriden-providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface MyFormValues { | |
captcha: number; | |
password: string; | |
username: string; | |
} | |
export interface MyForm extends FormGroup { | |
value: MyFormValues; | |
controls: { | |
captcha: AbstractControl; | |
password: AbstracControl; | |
username: AbstracControl; | |
} | |
} | |
/// | |
public myForm: MyForm | |
constructor(private formBuilder: FormBuilder) { } | |
ngOnInit() { | |
this.myForm: MyForm = this.formBuilder.group({ | |
captcha: ['', Validators.required], | |
usepasswordrname: ['', Validators.required], | |
username: ['', Validators.required], | |
}) | |
this.myForm.us // Now the 'username' property will be suggested | |
this.myForm.value.fakeControl // This will generate an error to make sure you don't try and access non-existent properties | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment