Form group dropdown menu content not present in json, Angular 6 - html

I have made a form group where the purpose is to register cars.
On submit it is generating a JSON from the values inserted.
The issue is that the value from the dropdown menu is not present in the JSON.
The dropdown menu is populated with the choices shown below and is the first entry in the form group.
The label names and error messages are in Norwegian but the essence should be there.
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-3 col-sm-12 offset-md-4">
<h3>Fyll inn</h3>
<form name="form" (ngSubmit)="f.form.valid && onSubmit()" #f="ngForm" novalidate>
<div class="form-group">
<label for="CarType">Bil type</label>
<select id="CarType" name="CarType" [(ngModel)]="CarType" class="form-control">
<option [ngValue]="CarType" *ngFor="let CarType of CarTypes" [value]="CarType.Id">{{CarType.Name}}</option>
</select>
</div>
<div class="form-group">
<label for="LicensePlate">Skiltnummer</label>
<input type="text" class="form-control" name="LicensePlate" [(ngModel)]="model.LicensePlate" #LicensePlate="ngModel" [ngClass]="{ 'is-invalid': f.submitted && LicensePlate.invalid }" required />
<div *ngIf="f.submitted && LicensePlate.invalid" class="invalid-feedback">
<div *ngIf="LicensePlate.errors.required">Skiltnummer er påkrevd</div>
</div>
</div>
<div class="form-group">
<label for="KilometersDriven">Kilometerstand</label>
<input type="number" class="form-control" name="KilometersDriven" [(ngModel)]="model.KilometersDriven" #KilometersDriven="ngModel" [ngClass]="{ 'is-invalid': f.submitted && KilometersDriven.invalid }" required />
<div *ngIf="f.submitted && KilometersDriven.invalid" class="invalid-feedback">
<div *ngIf="KilometersDriven.errors.required">Kilometerstand er påkrevd</div>
</div>
</div>
<div class="form-group">
<label for="Seats">Antall seter</label>
<input type="number" class="form-control" name="Seats" [(ngModel)]="model.Seats" #Seats="ngModel" [ngClass]="{ 'is-invalid': f.submitted && Seats.invalid }" required />
<div *ngIf="f.submitted && Seats.invalid" class="invalid-feedback">
<div *ngIf="Seats.errors.required">Antall seter er påkrevd</div>
</div>
</div>
<div class="form-group">
<label for="Gears">Antall gir</label>
<input type="number" class="form-control" name="Gears" [(ngModel)]="model.Gears" #Gears="ngModel" [ngClass]="{ 'is-invalid': f.submitted && Gears.invalid }" required />
<div *ngIf="f.submitted && Gears.invalid" class="invalid-feedback">
<div *ngIf="Gears.errors.required">Antall gir er påkrevd</div>
</div>
</div>
<div class="form-group">
<button class="btn btn-primary">Registrer</button>
</div>
</form>
</div>
</div>
</div>
</div>
import { Component, OnInit } from '#angular/core';
import { CarType } from 'src/shared/models/cartype.model';
#Component({
selector: 'app-add-car',
templateUrl: './add-car.component.html',
styleUrls: ['./add-car.component.css']
})
export class AddCarComponent implements OnInit {
CarTypes: CarType[] = [
{ Id: 1, Name: 'SUV' },
{ Id: 2, Name: 'Coupe' },
{ Id: 3, Name: 'Sedan' }
]
model: any = {};
onSubmit() {
console.log(this.model)
alert('SUCCESS \n\n' + JSON.stringify(this.model))
}
constructor() { }
ngOnInit() {
}
}
export class CarType {
Id: number;
Name: String;
}
To the right is a link to the fill in form on the website.
To the right is a image of the JSON reply created from the previous picture
{LicensePlate: "RH123123", KilometersDriven: 120000, Seats: 5, Gears: 6}

You didn't bind it with the model object [(ngModel)]="CarType"
<select id="CarType" name="CarType" [(ngModel)]="model.carType" class="form-control">
<option [ngValue]="CarType" *ngFor="let CarType of CarTypes" [value]="CarType.Id">{{CarType.Name}}</option>
</select>

Related

onChange function only working on second click

I have a form that I need the additional description input to show when 'other' is selected from the dropdown, however with the current function this only works on the second selection.
It currently works if a section is made, then cleared and then 'Other' is selected. I need this to fire and display the other box on the first click if required by the user.
HTML --
<form class="form-horizontal" [formGroup]="unableToScanForm" *ngIf="!loading" (ngSubmit)="onSubmit()">
<div class="container">
<div class="card-body">
<div class="form-group row">
<label class="col-md-4 col-form-label" for="reasonType"><strong>Reason*</strong></label>
<div class="col-md-12">
<ng-select id="reasonType" name="reasonType"
placeholder="Select a reason..."
labelForId="reasonType"
formControlName="reasonType"
[multiple]="false"
[hideSelected]="true"
[closeOnSelect]="true"
[items]="reasonType"
[searchable]="false"
bindLabel="type"
bindValue="type"
[ngClass]="{ 'is-invalid': submitted && f.reasonType.errors }">
</ng-select>
<div *ngIf="submitted && f.reasonType.errors" class="invalid-feedback">
Please select a reason.
</div>
</div>
</div>
<div class="form-group row" *ngIf="showAdditionalInfo">
<label class="col-md-4 col-form-label" for="reasonDescription"><strong>Additional Information*</strong></label>
<div class="col-md-12">
<textarea
rows="3"
class="form-control"
id="reasonDescription"
formControlName="reasonDescription"
[ngClass]="{ 'is-invalid': submitted && f.reasonDescription.errors }">
</textarea>
<div *ngIf="submitted && f.reasonDescription.errors" class="invalid-feedback">
Please detail a description of the reason.
</div>
</div>
</div>
</div>
</div>
</form>
TS --
reasonSelection(): void {
this.unableToScanForm.valueChanges.subscribe(value => {
if (value.reasonType === 'Other') {
this.showAdditionalInfo = true;
}
});
}
Suppose you have to start listen changes with the first form value:
reasonSelection(): void {
this.unableToScanForm.valueChanges
.pipe(startWith(this.unableToScanForm.value))
.subscribe(...);
}

Unable to find the error form is not responding after being rendered

I have created a form Which responded really fin but I don't know what things I have messed in it. it is not responding when I click on submit or reset buttons. I have gone through the code line by line thoroughly but found nothing. I have tried restoring the previous versions but was unable to solve it please help me.
Component.html
<div class="card m-3">
<div class="card-body">
<form [formGroup]="surveyForm" (ngSubmit)="onSubmit()">
<div class="form-group col-5">
<label>UserName:</label>
<input id="name" type="text" formControlName="fName" class="form-control" [ngClass]="{'is-invalid':submitted && f['fName'].errors}"/>
<div *ngIf="submitted && f['fName'].errors" class="invalid-feedback"></div>
<div *ngIf="submitted && surveyForm.controls['fName'].errors" class="form-control">first name is required</div>
</div>
<div class="form-group col-5">
<label>StudentID:</label>
<input placeholder="g01333314" id="StudentId" type="text" formControlName="StudentId" class="form-control" [ngClass]="{'is-invalid':submitted && f['StudentId'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['StudentId'].errors" class="form-control">Enter a valid student id</div>
</div>
<div class="form-group col-5">
<label>Street Address:</label>
<input id="StreetAddress" placeholder="4400 university drive" type="text" formControlName="StreetAddress" class="form-control" [ngClass]="{'is-invalid':submitted && f['StreetAddress'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['StreetAddress'].errors" class="form-control">Enter a valid Address.</div>
</div>
<div class="form-group col-5">
<label>City:</label>
<input id="City" type="text" placeholder="Fairfax" formControlName="City" class="form-control" [ngClass]="{'is-invalid':submitted && f['City'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['City'].errors" class="form-control">Enter a valid city name.</div>
</div>
<div class="form-group col-5">
<label>State:</label>
<input id="State" type="text" placeholder="VA" formControlName="State" class="form-control" [ngClass]="{'is-invalid':submitted && f['State'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['State'].errors" class="form-control">Enter a valid State.</div>
</div>
<div class="form-group col-5">
<label>Zip Code:</label>
<input id="Zip" type="text" placeholder="22030" formControlName="Zip" class="form-control" [ngClass]="{'is-invalid':submitted && f['Zip'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['Zip'].errors" class="form-control">Enter a valid Zip Code.</div>
</div>
<div class="form-group col-5">
<label>Telephone Number:</label>
<input id="Telephone" type="number" placeholder="7039932000" formControlName="Telephone" class="form-control" [ngClass]="{'is-invalid':submitted && f['Telephone'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['Telephone'].errors" class="form-control">Enter a valid Telephone number.</div>
</div>
<div class="form-group col-5">
<label>Email:</label>
<input id="Email" type="email" placeholder="abc#domain.com" formControlName="Email" class="form-control" [ngClass]="{'is-invalid':submitted && f['Email'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['Email'].errors" class="form-control">Enter a valid Email.</div>
</div>
<div class="form-group col-5">
<label>URL:</label>
<input id="Url" type="url" placeholder="https://www.google.com" formControlName="Url" class="form-control" [ngClass]="{'is-invalid':submitted && f['Url'].errors}"/>
<div *ngIf="submitted && surveyForm.controls['Url'].errors" class="form-control">Enter a URL</div>
</div>
<div class="text-center">
<button type="button" class="btn btn-primary mr-1" (click)="onSubmit()">Submit</button>
<button type="button" class="btn btn-secondary" (click)="onReset()">Reset</button>
</div>
</form>
</div></div>
Component.ts
import { Component, OnInit } from '#angular/core';
import { FormControl, FormBuilder, FormGroup, Validators} from '#angular/forms';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
surveyForm!: FormGroup;
submitted= false;
constructor(private formBuilder: FormBuilder){}
ngOnInit(){
const reg = '(https?://)?([\\da-z.-]+)\\(.|:)([a-z.]{2,6})[/\\w .-#$#%&]*/?';
this.surveyForm = this.formBuilder.group({
fName: ['',[Validators.required, Validators.pattern(/^[a-zA-Z ]{2,30}$/)]],
StudentId: ['', [Validators.required, Validators.pattern(/^[A-Za-z]{1,1}\d{8,8}$/)]],
StreetAddress: ['', [Validators.required, Validators.pattern(/^(?:[0-9]+\s[a-zA-Z]|[A-Za-z]+\s[0-9])[a-z0-9\s]*$/)]],
City: ['', [Validators.required, Validators.minLength(3), Validators.pattern(/^[a-zA-Z]*$/)]],
State: ['',[Validators.required, Validators.pattern(/(^[a-zA-Z]{2,2})*$/)]],
Zip: ['',[Validators.required, Validators.minLength(5), Validators.maxLength(5), Validators.pattern(/^[0-9]*$/)]],
Telephone: ['',[Validators.required, Validators.minLength(10), Validators.maxLength(10), Validators.pattern(/^[0-9]*$/)]],
Email: ['', [Validators.required, Validators.pattern(/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/)]],
Url: ['',[Validators.required, Validators.pattern(reg)]]
//Date: ['',[Validators.required]]
});
}
//name = new FormControl('');
get f() { return this.surveyForm.controls; }
onSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.surveyForm.invalid) {
alert('invalid details');
}
else
// display form values on success
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.surveyForm.value, null, 4));
}
onReset() {
this.submitted = false;
this.surveyForm.reset();
}
}
Something is wrong with your regex that you assign to your reg variable inside your ngOnInit. Your code works if you delete the validator that regex is used in.
ngOnInit(){
const reg = '(https?://)?([\\da-z.-]+)\\(.|:)([a-z.]{2,6})[/\\w .-#$#%&]*/?';
this.surveyForm = this.formBuilder.group({
fName: ['',[Validators.required, Validators.pattern(/^[a-zA-Z ]{2,30}$/)]],
StudentId: ['', [Validators.required, Validators.pattern(/^[A-Za-z]{1,1}\d{8,8}$/)]],
StreetAddress: ['', [Validators.required, Validators.pattern(/^(?:[0-9]+\s[a-zA-Z]|[A-Za-z]+\s[0-9])[a-z0-9\s]*$/)]],
City: ['', [Validators.required, Validators.minLength(3), Validators.pattern(/^[a-zA-Z]*$/)]],
State: ['',[Validators.required, Validators.pattern(/(^[a-zA-Z]{2,2})*$/)]],
Zip: ['',[Validators.required, Validators.minLength(5), Validators.maxLength(5), Validators.pattern(/^[0-9]*$/)]],
Telephone: ['',[Validators.required, Validators.minLength(10), Validators.maxLength(10), Validators.pattern(/^[0-9]*$/)]],
Email: ['', [Validators.required, Validators.pattern(/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/)]],
// This enclosed part of the line leads to your problem
// ----------------------------vvvvvvvvvvvvvvvvvvvvvvv---------------------
Url: ['',[Validators.required, Validators.pattern(reg)]]
// ----------------------------^^^^^^^^^^^^^^^^^^^^^^^---------------------
//Date: ['',[Validators.required]]
});
}
I'm not entirely sure on what is wrong with that regex - especially since you provide it as a string, not a regex. Maybe if you provide what that regex should do I can expand on this answer.
See this working StackBlitz, which contains your code with only that single line commented out.

How to verify validion of form inside form?

I'm trying to verify a form that's inside a form.
The idea is to create a user's html page with the option to create a contact list.
My goal is to verify both forms with one click on the 'Save User Profile' button.
My question is How to verify the validion of the inner form inside the main form?
NO BOOTSTRAP AND OTHERS.
Main form:
HTML:
<div class="container"
style="width: 60%;">
<h5>User Profile</h5>
<div>
<form (ngSubmit)="onSubmit()"
#f=ngForm>
<!-- Name -->
<div class="column">
<label for="name"
style="margin-left: 5px;">Name</label><br>
<input type="text"
[(ngModel)]="user.name"
name="name"
id="name"
required
minlength="2"
maxlength="30"
#n="ngModel">
<p *ngIf="!n.valid && n.touched"
style="color: red;"> Please insert a name with 2-30 chars</p>
</div>
<!-- Organization -->
<div class="column">
<label for="orga"
style="margin-left: 5px;">Organization</label><br>
<input type="text"
[(ngModel)]="user.organization"
name="orga"
id="orga">
</div>
<br><br><br><br><br>
<h5>Contact Information</h5>
<div class="box">
<app-user-card *ngFor="let c of contacts; let i = index"
[id]="i"
[user]="user"></app-user-card>
<br><br>
</div>
<button type="button"
class="btn-add"
(click)="addContact()">Add Contact</button>
<button class="btn-save"
type="submit"
[disabled]="!f.valid">Save User Profile
</button>
</form>
</div>
TS:
export class UsersListComponent implements OnInit {
users: User[] = []
user: User = {
name: '',
organization: '',
contacts: []
}
id: number
contacts: Contact[] = []
contact: Contact = {
id: 0,
type: '',
label: '',
contact: ''
}
constructor() { }
ngOnInit(): void {
this.user.contacts = this.contacts
}
addContact() {
this.contacts.push(new Contact(this.id, this.contact.type, this.contact.label, this.contact.contact))
}
onSubmit() {
/* Upgrade option: Check if a user exists on the system and view his contacts*/
this.users.push(new User(this.user.name, this.user.organization, this.contacts))
console.log(this.users);
}
}
inner form:
HTML:
<div class="card">
<form #f = "ngForm">
<div class="column">
<label for="contactType">Type of Contact</label><br>
<select id="contactType"
[(ngModel)]=contact.type
name="cotactType"
style="width: 95%;"
required>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
</div>
<div class="column">
<label for="label">Contact Label</label><br>
<input type="text"
[(ngModel)]="contact.label"
id="label"
name="lable"
style="width: 95%;"
required
minlength="2"
maxlength="20"
#l>
<p *ngIf="!l.valid && l.touched"
style="color: red;">Label is required!</p>
</div>
<div>
<label for="contact"
style="margin-left: 5px;">Email/Phon No.</label><br>
<input type="text"
[(ngModel)]=contact.contact
required
id="contact"
name="contact"
style="width: 98%;"
#c>
<p *ngIf="!c.valid && c.touched"
style="color: red;">Email/Phon No is required!</p>
</div>
<button type="button"
class="btn-remove"
style="left: 69%;position: relative;">Remove</button>
</form>
TS:
#Component({
selector: 'app-user-card',
templateUrl: './user-card.component.html',
styleUrls: ['./user-card.component.css']
})
export class UserCardComponent implements OnInit {
contact: Contact = {
id: 0,
type: '',
label: '',
contact: ''
}
#ViewChild("f") contactDetails: NgForm
#Input() id: number
constructor() { }
ngOnInit(): void {}
}

Angular form error-TypeError: Class constructor Validators cannot be invoked without 'new'

I'm getting the following angular form error -
ERROR TypeError: Class constructor Validators cannot be invoked without 'new'
at forms.js:1480
at Array.map (<anonymous>)
at _executeValidators (forms.js:1476)
at FormControl.validator (forms.js:1418)
at FormControl._runValidator (forms.js:4089)
at FormControl.updateValueAndValidity (forms.js:4050)
at new FormControl (forms.js:4656)
at FormBuilder.control (forms.js:8951)
at FormBuilder._createControl (forms.js:9011)
at forms.js:8990
Can someone please tell me what I am missing?
My HTML:
<form class="formLogin" [formGroup]="personalForm" (ngSubmit)="onSubmit()">
<div class="form-group rtl">
<label class="col-sm-3 control-label pull-right" for="mail"> your email</label>
<div class="col-sm-9">
<input type="text" formControlName="mail" [value]="person.Email" placeholder="myAddress#gmail.com" class="form-control ltr textBox" email [ngClass]="{ 'is-invalid': submitted && f.mail.errors }" />
<div *ngIf="submitted && f.mail.errors" class="invalid-feedback">
<div *ngIf="f.mail.errors.required">requird</div>
<div *ngIf="f.mail.errors.email">invalid address!</div>
</div></div>
</div>
<br><br>
<div class="form-group rtl">
<label class="col-sm-3 control-label pull-right" for="phone">your phone</label>
<div class="col-sm-9">
<input type="text" formControlName="phone" [value]="person.Phone" class="form-control ltr textBox" placeholder="000-0000000" [ngClass]="{ 'is-invalid': submitted && f.phone.errors }" />
<div *ngIf="submitted && f.phone.errors" class="invalid-feedback">
<div *ngIf="f.phone.errors.required">requird!</div>
<div *ngIf="f.phone.errors.pattern">digits only!</div>
</div></div>
</div>
<br><br>
<div class="form-group rtl">
<label class="col-sm-3 control-label pull-right" for="username">your username</label>
<div class="col-sm-9">
<input type="text" (focus)="incorrectData=false" [value]="person.UserName" formControlName="username" class="form-control textBox " [ngClass]="{ 'is-invalid': submitted && f.username.errors }" />
<div *ngIf="submitted && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">required!</div>
</div></div>
</div>
<br><br>
<div class="form-group rtl">
<label class="col-sm-3 control-label pull-right" for="password">your password</label>
<div class="col-sm-9">
<input type="password" (focus)="incorrectData=false" [value]="person.Password" minlength="6" formControlName="password" class="form-control textBox" [ngClass]="{ 'is-invalid': submitted && f.password.errors }" />
<div *ngIf="submitted && f.password.errors" class="invalid-feedback">
<div *ngIf="f.password.errors.required">requird!</div>
<div *ngIf="f.password.errors.minlength">password length minimum 6 letters!</div>
</div></div>
</div>
<br>
</form>
My ts:
import { Component, OnInit } from '#angular/core';
import { UserService } from '../../../services/user.service';
import { User,UserEnum } from 'src/app/classes/User';
import { FormBuilder, FormGroup,Validators} from '#angular/forms';
#Component({
selector: 'app-personal-details',
templateUrl: './personal-details.component.html',
styleUrls: ['./personal-details.component.css']
})
export class PersonalDetailsComponent implements OnInit {
person:User;
personalForm:FormGroup;
submitted :boolean;
constructor(private userSer:UserService,
private formBuilder: FormBuilder){ }
ngOnInit() {
this.person=this.userSer.userDetailes;
this.personalForm = this.formBuilder.group({
email:['',[Validators.required,Validators.email]],
password: ['', [Validators.required,Validators.minLength(6),Validators]],
username: ['', Validators.required],
phone:['',[Validators.required,Validators.maxLength(10),Validators.minLength(9),Validators.pattern('[0-9]*')]]});}
get f()
{ return this.personalForm.controls; }
saveUser() {}
onSubmit()
{
this.submitted = true;
// stop here if form is invalid
if (this.personalForm.invalid)
{return;}
this.saveUser();}
}
userService:
#Injectable({
providedIn: 'root'
})
export class UserService {
userDetailes:User=null;
}
I have good form imports for my app.module and other forms in my project work well. I think the problem is in this component.
instead of:
password: ['',
[Validators.required,Validators.minLength(6),Validators]],
i had to write:
password: ['',
[Validators.required,Validators.minLength(6),Validators.maxLength(10)]],
just as written above, your problem is because you are referencing the validators in a wrong manner. You need to include the method you are trying to call or invoke. In this case, your line
password: ['', [Validators.required,Validators.minLength(6),Validators]],
Should be
password: ['', [Validators.required,Validators.minLength(6),Validators.maxLength(20)]],// or any other measure you wish to put in place. Validators is the problem here

Angular 4 Reset Button Throws Error

I have the following form in my HTML element:
<form class="row" name="powerPlantSearchForm" (ngSubmit)="f.valid && searchPowerPlants()" #f="ngForm" novalidate>
<div class="form-group col-xs-3" >
<label for="powerPlantName">PowerPlant Name</label>
<input type="text" class="form-control-small" [ngClass]="{ 'has-error': f.submitted && !powerPlantName.valid }" name="powerPlantName" [(ngModel)]="model.powerPlantName" #powerPlantName="ngModel" />
</div>
<div class="form-group col-xs-3" >
<label for="powerPlantType">PowerPlant Type</label>
<select class="hideLabel form-control" [(ngModel)]="model.powerPlantType" name="powerPlantType" (change)="selectName();">
<option selected="" value="">--Select Type--</option>
<option [ngValue]="powerPlantType" *ngFor="let powerPlantType of powerPlantTypes">
{{ powerPlantType }}
</option>
</select>
</div>
<div class="form-group col-xs-3" [ngClass]="{ 'has-error': f.submitted && !organizationName.valid }">
<label for="organizationName">Organization Name</label>
<input type="text" class="form-control-small" name="powerPlantOrganization" [(ngModel)]="model.powerPlantOrg" #organizationName="ngModel" />
</div>
<div class="form-group col-xs-3" [ngClass]="{ 'has-error': f.submitted && !powerPlantStatus.valid }">
<label for="powerPlantStatus">PowerPlant Active Status</label>
<input type="text" class="form-control-small" name="powerPlantStatus" [(ngModel)]="model.isOnlyActivePowerPlants" #powerPlantStatus="ngModel" />
</div>
<div class="form-group col-md-3 col-xs-4">
<button [disabled]="loading" class="btn btn-primary">Search For PowerPant's...</button>
<img *ngIf="loading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
</div>
<div class="form-group col-md-3 col-xs-3">
<button class="btn btn-primary" (click)="reset(f)">Reset Search Criteria</button>
</div>
</form>
It renders fine, but when I tried to click the reset button, I get the following error:
ERROR TypeError: _co.reset is not a function
at Object.eval [as handleEvent] (HomeComponent.html:35)
at handleEvent (core.es5.js:12004)
at callWithDebugContext (core.es5.js:13465)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13053)
at dispatchEvent (core.es5.js:8602)
at core.es5.js:9213
at HTMLButtonElement.<anonymous> (platform-browser.es5.js:2651)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
at Object.onInvokeTask (core.es5.js:3881)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
Here is my component class:
export class HomeComponent implements OnInit {
// Represents the PowerPlantTypes
powerPlantTypes = ['RampUpType', 'OnOffType'];
// Represents the search form
model: any = {};
// currentUser: User;
// represents the list of PowerPlant data
powerPlants: PowerPlant[];
users: User[] = [];
constructor(private userService: UserService, private powerPlantService: PowerPlantService) {
// this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
}
ngOnInit() {
this.allPowerPlants();
}
selectName() {
alert(this.model.powerPlantType);
}
searchPowerPlants(): void {
const powerPlantSearchParams = new PowerPlantSearchParams(
this.model.powerPlantType,
this.model.powerPlantOrganization,
this.model.powerPlantName,
this.model.page,
this.model.powerPlantStatus);
this.powerPlantService.searchPowerPlants(powerPlantSearchParams).subscribe(result => {
this.powerPlants = <PowerPlant[]> result;
});
}
allPowerPlants(onlyActive: boolean = false, page: number = 1): void {
this.powerPlantService.allPowerPlants(onlyActive, page).subscribe(result => {
this.powerPlants = <PowerPlant[]> result;
});
}
}
What is this error conveying?
According to the Angular docs it seems you should call f.reset() not reset(f)
Check your component class if the function named reset actually exists.
use following code with Reset Search Criteria button
(click)='form.reset()'
no parameter is required
(click)="reset(form)" // wrong