Angular Material mat-stepper not working with 4 child items - html

I'm trying to create a mat-stepper on a new, empty page in Angular
it does work generally, but disappears when I add the 4th mat-from-field (in step 2)
the whole mat-stepper won't show up anymore after refreshing the page
input fields are all bound to a variable through "[(ngModel)]"
every mat-step has it's own "[stepControl]"
every form has it's own "[formGroup]"
HTML:
<mat-stepper linear labelPosition="bottom" #stepper>
<mat-step [stepControl]="firstFormGroup" [editable]="true">
<form [formGroup]="firstFormGroup" class="groupFields">
<ng-template matStepLabel>Fill out Project Info</ng-template>
<mat-form-field appearance="fill">
<mat-label>Project Name</mat-label>
<input matInput formControlName="firstCtrl" [(ngModel)]="projectName" required>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Requirements</mat-label>
<textarea [(ngModel)]="requirements"
matInput
cdkTextareaAutosize
#autosize="cdkTextareaAutosize"
cdkAutosizeMinRows="5"
cdkAutosizeMaxRows="15"></textarea>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Employee Email</mat-label>
<input matInput [(ngModel)]="email" required>
</mat-form-field>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step [stepControl]="secondFormGroup" [editable]="true">
<form [formGroup]="secondFormGroup">
<div class="customerFrom">
<div class="groupFields">
<ng-template matStepLabel>Fill out Customer Info</ng-template>
<mat-form-field appearance="fill">
<mat-label>Customer Name</mat-label>
<input matInput formControlName="secondCtrl" [(ngModel)]="company" required>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Address</mat-label>
<input [(ngModel)]="customerAddress" matInput>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>City</mat-label>
<input [(ngModel)]="customerCity" matInput>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Customer Type</mat-label>
<input [(ngModel)]="customerType" matInput>
</mat-form-field>
</div>
</div>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step [stepControl]="thirdFormGroup" [editable]="true">
<form [formGroup]="thirdFormGroup">
<div class="customerFrom">
<div class="groupFields">
<ng-template matStepLabel>Add Contacts</ng-template>
<mat-form-field appearance="fill">
<mat-label>Contact Stuff</mat-label>
<input formControlName="thirdCtrl" matInput>
</mat-form-field>
</div>
</div>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step>
<ng-template matStepLabel>Create Project</ng-template>
Create the Project or go back and edit!
<div>
<button mat-button matStepperPrevious>
<mat-icon>edit</mat-icon> back
</button>
<button mat-button (click)="createProject()">
<mat-icon>done</mat-icon> create Project
</button>
</div>
</mat-step>
</mat-stepper>
TS:
export class ProjectCreationComponent implements OnInit {
projectName: string = '';
selectedStatus: string = '';
requirements: string = '';
company: string = '';
customerAddress: string = '';
customerCity: string = '';
customerType: string = '';
forename: string = '';
surname: string = '';
address: string = '';
city: string = '';
email: string = '';
mobile: string = '';
contacts: Contact[] = [];
unsubscribe$: Subject<void> = new Subject<void>();
firstFormGroup = this._formBuilder.group({
firstCtrl: ['', Validators.required],
});
secondFormGroup = this._formBuilder.group({
secondCtrl: ['', Validators.required],
});
thirdFormGroup = this._formBuilder.group({
thirdCtrl: ['', Validators.required],
});
constructor(
private service: ProjectService,
private router: Router,
private _formBuilder: FormBuilder) { }
ngOnInit(): void {
}
addContact() {
let contact = {} as Contact;
contact.forename = this.forename;
contact.surname = this.surname;
contact.address = this.address;
contact.city = this.city;
contact.email = this.email;
contact.mobile = this.mobile;
this.contacts.push(contact);
}
deleteContact(contact: Contact) {
const index: number = this.contacts.indexOf(contact);
if(index !== -1) {
this.contacts.splice(index, 1);
}
}
createProject() {
const customer: Customer = {
name: this.company,
contacts: this.contacts,
address: this.customerAddress,
city: this.customerCity,
customerTyp: this.customerType
}
const announcement: Announcement = {
requirements: this.requirements,
customers: [customer],
offers: [],
}
const project: ProjectCreationRequest = {
name: this.projectName,
employeeEmail: this.email,
announcements: [announcement],
offers: []
};
this.service.createProject(project)
.pipe(takeUntil(this.unsubscribe$))
.subscribe();
this.router.navigateByUrl('/');
}
ngOnDestroy() {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
}
I've also exactly tried removing a mat-form-field (so that I only have 3 in the form), for every single one of them, so there is not a specific one that's causing the problem.
One more thing I've tried is changing "[(ngModel)]" to "[value]", because 2 way binding isn't necessarily needed, but with that the value of the textfield can't be stored in the variable.
Also I didn't find anything in the material documentation about a maximum size of step child items or anything.
Stackblitz (not 1:1 because of too many classes): https://stackblitz.com/edit/angular-buspzu?file=src/app/app.component.ts

Related

how to add new event in calendar and apply drag and drop to it using angular cdk?

I am using NgxEventCalendarModule to create an event calendar to do the following task:
User can add a new event
user can edit the events
user can see the events
user can drag and drop event
here is my app.component.html
<div style="height: 100%; width: 100%;border: 1px solid gray;" >
<ngx-event-calendar
[showAddButton]="true"
[language]="'en'"
[dataSource]="dataArray"
(dayEvents)="selectDay($event)"
(newEvent)="addEvent($event)">
</ngx-event-calendar >
</div>
and app.component.ts
export class AppComponent {
title = 'de-calendar';
language: string = 'en';
dataArray: IEventData[] = testData;
constructor(public dialog: MatDialog) { }
addEvent(event) {
alert(event);
}
dayEvents(event) {
}
selectDay(event) {
if (event.events) {
const dialogRef = this.dialog.open(AddEventDialogComponent, {
width: '80%',
height: '90%',
data: event
});
}
}
IEventData.ts
export interface IEventData {
id: number;
title: string;
desc?: string;
startDate: Date;
endDate: Date;
createdBy?: string;
createdAt?: Date;
type?: number;
color?: string;
}
testData.ts
import { IEventData } from '../Interface/IEventData';
export const testData: IEventData[] = [
{ id: 20, title: 'Meeting with Manager', desc: 'Talk abut the new Task',
startDate: new Date("2022-12-22T21:00:00"), endDate: new Date("2022-12-26T23:00:00"), createdBy: 'Mark',
createdAt: new Date("2022-12-22T10:00:00"), type: 2, color: 'red' },
{ id: 12, title: 'Play Footbal', desc: 'Play FootBall wih freinds',
startDate: new Date("2022-12-19T13:43:00"), endDate: new Date("2022-12-23T15:00:00"), createdBy: 'Mark',
createdAt: new Date("2022-12-19T10:00:00"), type: 2, color: 'green' },
];
AddEventDialogComponent
export class AddEventDialogComponent implements OnInit {
startDate;
startHours;
startMinutes;
endDate;
endHours;
endMinutes;
hours:any;
minutes:any;
colours;
newEvent = {} as IEventData;
constructor(public dialogRef: MatDialogRef<EventDialogComponent>) { }
ngOnInit() {
this.hours = hours;
this.minutes = minutes;
this.colours = colours;
}
btn_SaveClick() {
this.startDate.setHours(this.startDate.getHours() + this.startHours)
this.startDate.setMinutes(this.startDate.getMinutes() + this.startMinutes)
this.endDate.setHours(this.endDate.getHours() + this.endHours)
this.endDate.setMinutes(this.endDate.getMinutes() + this.endMinutes);
this.newEvent.startDate = this.startDate;
this.newEvent.endDate = this.endDate;
this.dialogRef.close(AddEventDialogComponent);
}
AddEventDialogComponent.html
<h4>Add New Event</h4>
<mat-divider></mat-divider>
<div class="container">
<div class="first">
<mat-form-field class="date-picker">
<input readonly matInput [matDatepicker]="picker1" placeholder="Start date:" [(ngModel)]="startDate" (click)="picker1.open()">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker touchUi="true" #picker1></mat-datepicker>
</mat-form-field>
<mat-form-field class="date-picker">
<mat-label>Hours</mat-label>
<mat-select [(ngModel)]="startHours">
<mat-option *ngFor="let h of hours" [value]="h.id">{{ h.name }}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="date-picker">
<mat-label>Minutes</mat-label>
<mat-select [(ngModel)]="startMinutes">
<mat-option *ngFor="let m of minutes" [value]="m.id">{{ m.minute }}</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="first">
<mat-form-field class="date-picker">
<input readonly matInput [matDatepicker]="picker2" placeholder="End date:" [(ngModel)]="endDate" (click)="picker2.open()">
<mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker touchUi="true" #picker2></mat-datepicker>
</mat-form-field>
<mat-form-field class="date-picker">
<mat-label>Hours</mat-label>
<mat-select [(ngModel)]="endHours">
<mat-option *ngFor="let h of hours" [value]="h.id">{{ h.name }}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="date-picker">
<mat-label>Minutes</mat-label>
<mat-select [(ngModel)]="endMinutes">
<mat-option *ngFor="let m of minutes" [value]="m.id">{{ m.minute }}</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="second">
<mat-form-field class="example-full-width" (keydown.enter)="btn_SaveClick()">
<input matInput placeholder="Title:" [(ngModel)]="newEvent.title">
</mat-form-field>
<mat-form-field class="example-full-width" (keydown.enter)="btn_SaveClick()">
<input matInput placeholder="Description:" [(ngModel)]="newEvent.desc">
</mat-form-field>
<mat-form-field class="example-full-width" (keydown.enter)="btn_SaveClick()">
<input matInput placeholder="Created By:" [(ngModel)]="newEvent.createdBy">
</mat-form-field>
<mat-form-field class="example-full-width">
<mat-label>Colours</mat-label>
<mat-select [(ngModel)]="newEvent.color">
<mat-option *ngFor="let color of colours" [value]="color.value">{{ color.name }}</mat-option>
</mat-select>
</mat-form-field>
<button mat-raised-button color="primary" (click)="btn_SaveClick()"
(keydown.enter)="btn_SaveClick()" [disabled]="!startDate || !startHours || !startMinutes || !endDate || !endHours ||
!endMinutes || !newEvent.title || (startDate > endDate) || (startDate >= endDate && startHours > endHours) ||
(startDate >= endDate && startHours >= endHours && startMinutes >= endMinutes)">SAVE</button>
</div>
</div>
the testData successfully works for me and shows the data on a calendar
but when I want to add a new event from the Interface it does not add the event!!!
here is the running app picture
can anyone help me how to add a new event from Interface and apply drag and drop to the event using angular CDK?????

How to read JSON file in Angular cli?

I have an Angular cli project where I want to read data from a JSON file. These datas should be displayed on form's fields when the form is opened by user. What I have tried so far:
EmailSettingsComponent.ts
import { Component, OnInit, ViewChild, ViewEncapsulation, ChangeDetectorRef, Inject } from '#angular/core';
import * as FileSaver from 'file-saver';
import { MatDialog, MAT_DIALOG_DATA } from '#angular/material';
#Component({
selector: 'app-email-settings',
templateUrl: './email-settings.component.html',
styleUrls: ['./email-settings.component.css']
})
export class EmailSettingsComponent implements OnInit {
constructor() { }
smtpSettings: any;
saveJSON() {
var saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
// a.style = "display: none";
return function (data, fileName) {
//saving JSON code
};
}());
let smtpHost = (document.getElementById("smtpHost") as HTMLInputElement).value;
let smtpPort = (document.getElementById("smtpPort") as HTMLInputElement).value;
let smtpUserName = (document.getElementById("smtpUserName") as HTMLInputElement).value;
let smtpPassword = (document.getElementById("smtpPassword") as HTMLInputElement).value;
let smtpFrommtpHost = (document.getElementById("smtpFrom") as HTMLInputElement).value;
let smtpDisplayName = (document.getElementById("smtpDisplayName") as HTMLInputElement).value;
let smtpRecipients = (document.getElementById("smtpRecipients") as HTMLInputElement).value;
var data = { "Host": smtpHost, "Port": smtpPort, "UserName": smtpUserName, "UserPassword": smtpPassword, "From": smtpFrommtpHost, "DisplayName": smtpDisplayName, "Recipients":smtpRecipients, d: new Date() },
fileName = "my-download.json";
saveData(data, fileName);
}
ngOnInit(): void {
fetch('./assets/my-download.json').then(res => res.json())
.then(json => {
this.smtpSettings = json;
});
}
}
EmailSettingsComponent.html
<h1 mat-dialog-title>Email Settings</h1>
<div mat-dialog-content style="width: auto; height: auto">
<form id="formID" class="example-form" *ngFor="let item of smtpSettings">
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>SMTP Host</mat-label>
<textarea id="smtpHost" matInput placeholder="Ex: smtp.gmail.com">{{item.Host}}</textarea>
</mat-form-field>
<br />
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>SMTP Port</mat-label>
<textarea id="smtpPort" matInput placeholder="Ex: 587"></textarea>
</mat-form-field>
<br />
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>SMTP User Name</mat-label>
<textarea id="smtpUserName" matInput placeholder="Ex: example#gmail.com"></textarea>
</mat-form-field>
<br>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>SMTP User Password</mat-label>
<textarea id="smtpPassword" matInput placeholder="Ex: password"></textarea>
</mat-form-field>
<br>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>SMTP From</mat-label>
<textarea id="smtpFrom" matInput placeholder="Ex: example#gmail.com"></textarea>
</mat-form-field>
<br>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>SMTP Display Name</mat-label>
<textarea id="smtpDisplayName" matInput placeholder="Ex: Jhon"></textarea>
</mat-form-field>
<br>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Recipients</mat-label>
<textarea id="smtpRecipients" matInput placeholder="Ex: someone#email.com; somebody#test.com"></textarea>
</mat-form-field>
<br>
<button mat-button (click)="saveJSON()">Save</button>
</form>
</div>
With the above code, I'm getting Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays..
Based on kva's reply on accepted answer of this post, I have replaced smtpSettings: any; with smtpSettings: any=[];. Following this action, I get rid of the above error, but I get another one which is Error trying to diff '[object Object]'. Only arrays and iterables are allowed.
How could I read the data from JSON and populate the form with it when the form is opened?

dropdown and radio button form array value is not populating while editing in angular

In my form dropdown and radio button value is not populating while editing. I have tried using FormArray. But is still not getting the value in the dropdown and radio button.
HTML
<form [formGroup]="staffRegistrationForm">
<mat-label>Role</mat-label>
<mat-select formControlName="roleCode" required>
<mat-option *ngFor="let item of roleList" [value]="item.roleCode">
<span> {{item.description}} </span>
</mat-option>
</mat-select>
<mat-label>STD/ City Code</mat-label>
<mat-select formControlName="homeTelephoneCityCode" required>
<mat-option *ngFor="let phoneExt of phoneExtRefList"
[value]="phoneExt.value">
<span>{{phoneExt.name}} </span>
</mat-option>
</mat-select>
<div class="radio-group">
<mat-label>Staff ID * </mat-label>
<mat-radio-group formControlName="staffId">
<mat-radio-button [value]="id" *ngFor="let id of staffIds">
<span> {{id}} </span>
</mat-radio-button>
</mat-radio-group>
</div>
</form>
My component is
export class StaffRegistrationComponent implements OnInit {
staffRegistrationObj: StaffRegistrationObj = new StaffRegistrationObj();
roleList: Array<Role>;
phoneExtRefList = PHONE_EXT_LIST;
staffRegistrationForm: FormGroup;
submitted = false;
staffIds = [];
edit = false;
constructor(
private formBuilder: FormBuilder,
private staffService: StaffService,
) {
this.staffRegistrationForm = this.formBuilder.group({
staffId: [null, [Validators.required]],
role: this.formBuilder.group({[]}),
homeTelephoneCityCode: [null, [Validators.required]],
});
this.staffIds = ['Email', 'Mobile'];
const id = this.activatedRoute.snapshot.paramMap.get('id');
if (id !== null) {
this.getStaffDetails(id);}}}
onSave() {
private getStaffDetails(id: string) {
this.staffService.getStaffDetails(id).subscribe(
(staff: StaffRegistrationObj) => this.editStaffDetails(staff));}
private editStaffDetails(staff: StaffRegistrationObj) {
this.staffRegistrationForm.patchValue({
staffId: staff.staffId,
role: staff.role,
homeTelephoneCityCode: staff.homeTelephoneCityCode,});}
Please help me how to populate the value.

register an angular form in a nested json

I am trying to convert a html form to a nested json.
These are my classes :
export class Config {
id: string;
name: string;
email: string;
lchart:ComponentLinechart;
}
export class ComponentLinechart {
name_linechart : String;
xAxis_linechart : String;
yAxis_linechart : String;
}
The formcomponent.ts:-
export class FormsComponent implements OnInit {
newConfig: Config = new Config();
constructor(private service : MyserviceService, private configservice:ConfigurationService) {
}
email = new FormControl('', [Validators.required, Validators.email]);
xAxisControl = new FormControl('', Validators.required);
yAxisControl = new FormControl('', Validators.required);
name_linechart = new FormControl('', Validators.required);
name = new FormControl('', Validators.required);
getConfig(): void {
this.configservice.getConfig()
.subscribe(data => this.configs = data );
}
createConfiguration(todoForm: NgForm): void {
this.configservice.createConfig(this.newConfig)
.subscribe(createconfig => {
todoForm.reset();
this.newConfig = new Config();
this.configs.unshift(createconfig)
});
}
The formcomponent.html :-
<form #todoForm="ngForm" (ngSubmit)="createConfiguration(todoForm)" novalidate>
<div class="example-container">
<mat-form-field appearance="fill">
<mat-label>Enter your name</mat-label>
<input matInput [(ngModel)]="newConfig.name" name="name" [formControl]="name" required>
</mat-form-field>
<br>
<mat-form-field appearance="fill">
<mat-label>Enter your email</mat-label>
<input matInput placeholder="pat#example.com" [(ngModel)]="newConfig.email" name="email"
[formControl]="email" required>
<mat-error *ngIf="email.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
<br>
<mat-form-field appearance="fill">
<mat-label>Name-linechart</mat-label>
<input matInput [(ngModel)]="newConfig.name_linechart" name="name_linechart"
[formControl]="name_linechart" required>
</mat-form-field>
<br>
<mat-form-field *ngIf = "sales" >
<mat-label>xAxis-linechart</mat-label>
<mat-select [(ngModel)]="newConfig.xAxis-linechart" name="xAxis-linechart"
[formControl]="xAxisControl" required>
<mat-option *ngFor="let field of fields" [value] = "field">
{{field}}
</mat-option>
</mat-select>
<mat-error *ngIf="xAxisControl.hasError('required')">Please choose a field</mat-error>
</mat-form-field>
<br>
<mat-form-field *ngIf = "sales" >
<mat-label>yAxis-linechart</mat-label>
<mat-select [(ngModel)]="newConfig.yAxis-linechart" name="yAxis-linechart"
[formControl]="yAxisControl" required>
<mat-option *ngFor="let field of fields" [value] = "field">
{{field}}
</mat-option>
</mat-select>
<mat-error *ngIf="yAxisControl.hasError('required')">Please choose a field</mat-error>
</mat-form-field>
Expected result :
{
"name": "adam",
"email": "adam#gmail.com",
"lchart": {
"name_linechart": "books",
"xAxis_linechart": "library",
"yAxis_linechart": "percentage"
}
}
But this is what I get :
{
"name": "adam",
"email": "adam#gmail.com",
"lchart": null
}
I tried to write newConfig.lchart.name_linechart in the formcomponent.html but it gives me the error :
TypeError : cannot read property name_linechart of undefined.
Foufa, NEVER, NEVER, NEVER, NEVER use [(ngModel)] and formControlName (or formControl) in the same tag. One is for template Form, another for ReactiveForms, see the docs
Well. You has an object that has properties, one of them is an object, so, you has a FormGroup with somes FormControls and one FormGroup, (again the docs)
myForm=new FormGroup({
name:new FormControl(),
email:new FormControl(),
lchart:new FormGroup({
name_linechart: new FormControl(),
xAxis_linechart: new FormControl(),
yAxis_linechart: new FormControl(),
})
})
And the .html
<form [formGroup]="myForm">
<!--see, under the formGroup, we using formControlName for the formControl-->
<input formControlName="name">
<input formControlName="email">
<!--when we has a FomgGroup, we use formGrpupName in a div-->
<div formGroupName="lchart">
<!--and formControlName under the div -->
<input formControlName="name_linechart">
<input formControlName="xAxis_linechart">
<input formControlName="yAxis_linechart">
</div>
</form>
<!--we can add, only for check-->
<pre>
{{myForm?.value|json}}
</pre>
Update as always, is util use a function that received an object and create the form
getForm(data:any)
{
data=data || { name:null,
email:null,
lchart:{
name_linechart:null,
xAxis_linechart:null,
yAxis_linechart:0
}
}
return new FormGroup({
name:new FormControl(data.name,Validator.required),
email:new FormControl(data.email,[Validator.required,Validators.emailValidator),
lchart:new FormGroup({
name_linechart: new FormControl(data.lchart.name_linechart),
xAxis_linechart: new FormControl(data.lchart.xAxis_linechart),
yAxis_linechart: new FormControl(data.lchart.yAxis_linechart),
})
}
And use as
myForm=this.getForm(data) //<--if we has an object "data"
//or
myForm=this.getForm(null) //if we want a empty form
I guess your [(ngModel)] binding wrong for name_linechart,xAxis_linechart,yAxis_linechart fields.
It should be
[(ngModel)]="newConfig.lchart.name_linechart"
[(ngModel)]="newConfig.lchart.xAxis_linechart"
[(ngModel)]="newConfig.lchart.yAxis_linechart"
change your constructor to-
constructor(private service : MyserviceService, private configservice:ConfigurationService) {
this.newConfig.lchart=new ComponentLinechart(); //add this line
}

post empty value from autocomplete material typescript

I want to post data from autocomplete material.
My ts code, like this. I used registerUserForm formgroup.
export class AddUserFormComponent implements OnInit {
countryes: Country[];
registerUserForm: FormGroup;
filteredOptionsCountry: Observable<Country[]>;
myControlCountry: FormControl = new FormControl();
constructor(private fb: FormBuilder,
private router: Router,
private cs: CountryService)
{
this.registerUserForm = new FormGroup({
'username': new FormControl(),
'email': new FormControl(),
'country_id': new FormControl(),
});
}
ngOnInit() {
this.registerUserForm = this.fb.group({
'username': ['', Validators.compose([Validators.required, Validators.minLength(5)])],
'country_id': ['', Validators.required],
'email': ['', [Validators.required, ValidationService.emailValidation]],
});
this.filteredOptionsCountry = this.myControlCountry.valueChanges.pipe(
startWith(''),
map(val => this.filterCountry(val))
);
this.cs.getAllCountry().subscribe(
countryes => {
this.countryes = countryes.map((country) => {
return new Country(country);
});
}
);
}
onRegisterUser() {
this.loading = true;
this.invalidInputs = true;
let newUser = new User(
this.registerUserForm.value
);
this.userService.createUser(newUser).subscribe(
);
}
onCancel() {
this.router.navigate(['/main/users']);
}
//Country
filterCountry(val: string): Country[] {
if (val) {
let filterValue = val.toLowerCase();
console.log(this.countryes)
return this.countryes.filter(country => country.name.toLowerCase().startsWith(filterValue));
}
return this.countryes;
}
}
my html code. In this code i have 3 parameters, only email and username i can post, country_id post empty
<form [formGroup]="registerUserForm" (ngSubmit)="onRegisterUser()" class="col s12" materialize>
<div class="row">
<div class="input-field col s12">
<input formControlName="username" id="username" type="text" class="validate" placeholder="Enter Username" minlength="3" maxlength="20"
required="" [ngClass]="{invalid: invalidInputs}">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input formControlName="email" id="email" type="email" class="validate" placeholder="Enter Email" required="" aria-required="true"
[ngClass]="{invalid: invalidInputs}">
</div>
</div>
<!-- Autocomplete Country Material-->
<input formControlName="country_id" id="country_id" matInput placeholder="Select Country" aria-label="State" [matAutocomplete]="auto"
autoActiveFirstOption [formControl]="myControlCountry">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let country of filteredOptionsCountry | async" [value]="country.name">
{{ country.name }}
</mat-option>
</mat-autocomplete>
<!-- End Autocomplete Country -->
<div id="register_user_button_container" class="row">
<button id="register_user_button" type="submit" class="btn waves-effect waves-light">
Register
</button>
<button id="cancel_button" (click)="onCancel()" class="btn waves-effect waves-light grey lighten-4 black-text">
Cancel
</button>
</div>
</form>
Can you suggest me, how to used this FormControl inside registerUserForm ? Or, something solution.
Your code is litteral chaos.
First, group everything in a single form.
registerUserForm: FormGroup;
Then, instantiate your form only once, you don't need to do it more.
constructor() {
this.registerUserForm = this.fb.group({
username: ['', [Validators.required, Validators.minLength(5)]],
country_id: ['', Validators.required],
email: ['', [Validators.required, ValidationService.emailValidation]],
myControlCountry: ''
});
}
Next, use a getter to get your countries. (This is one of the many ways)
countries: Country[];
get filteredCountries() {
const query = this.registerUserForm.get('country_id').value;
return query ?
this.countries.filter(c => c.name.toLowerCase().includes(query.toLowerCase)) :
this.countries;
}
Now you must bind it to your HTML :
<mat-option *ngFor="let country of filteredCountries" [value]="country.name">
{{ country.name }}
</mat-option>