Skip to content

Instantly share code, notes, and snippets.

@ssuperczynski
Last active October 17, 2018 15:55
Show Gist options
  • Save ssuperczynski/9db044a0ae69fa8feb5fb9f82c99f6ce to your computer and use it in GitHub Desktop.
Save ssuperczynski/9db044a0ae69fa8feb5fb9f82c99f6ce to your computer and use it in GitHub Desktop.
import { OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
export interface CommonAddress {
id: number;
name: string;
}
export const FORM_PARAMS = {
supervisors: 'supervisors',
};
export class RecruitmentEditComponent implements OnInit {
public recruitmentForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.recruitmentForm = this.fb.group({
supervisors: this.fb.array([]),
});
}
get supervisors(): FormArray {
return this.recruitmentForm.get(FORM_PARAMS.supervisors) as FormArray;
}
// You will not get any error if `_.id type is not equal to item.id`
selectSupervisor(item: any) {
const defined = this.supervisors.getRawValue().some(_ => _.id === item.id);
if (!defined) {
this.supervisors.push(new FormControl({
id: item.id,
name: item.name,
}));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment