Skip to content

Instantly share code, notes, and snippets.

View Armenvardanyan95's full-sized avatar

Armen Vardanyan Armenvardanyan95

View GitHub Profile
@Directive({
selector: '[formControl]'
})
export class TrimDirective implements OnInit {
constructor(
private ngControl: NgControl,
) { }
ngOnInit() {
interface RawFormValue {
firstName: string;
lastName: string;
age: number;
}
function toRawFormValue<T extends RawFormValue>(serverData: T): RawFormValue {
return {
firstName: serverData.firstName,
lastName: serverData.lastName,
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
export class AppComponent {
form = this.formBuilder.group({
firstName: [''],
lastName: [''],
age: [''],
});
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
export class AppComponent {
form = this.formBuilder.group({
firstName: [''],
lastName: [''],
age: [''],
});
<custom-component formControlName="controlName"></custom-component>
export class AppComponent {
// rest of the component implementation is ommitted for brevity
submit() {
if (this.form.valid) {
const article = new ArticleDTO(this.form.value as RawFormValue);
// now send the article DTO to the backend using one of your services
}
}
}
export class ArticleDTO {
title: string;
tags: string;
date: string;
referenceIds: number[];
constructor(formValue: RawFormValue) {
this.title = formValue.title;
this.tags = formValue.tags.join(',');
this.date = formValue.date.toISOString();
<form [formGroup]="form">
<div>
<label for="firstName">First Name</label>
<input formControlName="firstName" id="firstName"/>
<span *ngIf="controls.lastName.touched && controls.firstName.hasError('required')" class="errors">
Field is required
</span>
</div>
<div>
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
export class AppComponent {
form = this.formBuilder.group({
firstName: [''],
lastName: [''],
age: [''],
});
<form [formGroup]="form">
<div>
<label for="firstName">First Name</label>
<input formControlName="firstName" id="firstName"/>
<span *ngIf="form.get('firstName').touched && form.get('firstName').hasError('required')" class="errors">
Field is required
</span>
</div>
<div>