我有以下HTML表单:
<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="form-control" [(ngModel)]="model.powerPlantType" name="powerPlantType">
<option value="" disabled>--Select Type--</option>
<option [ngValue]="powerPlantType" *ngFor="let powerPlantType of powerPlantTypes">
{{ powerPlantType }}
</option>
</select>
</div>
<div class="form-group col-xs-3" >
<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" >
<label for="powerPlantStatus">PowerPlant Active Status</label>
<select class="form-control" [(ngModel)]="model.powerPlantStatus" name="powerPlantStatus">
<option value="" disabled>--Select Status--</option>
<option [ngValue]="powerPlantStatus" *ngFor="let powerPlantStatus of powerPlantStatuses">
{{ powerPlantStatus }}
</option>
</select>
</div>
<div class="form-group col-md-3 col-xs-4">
<button [disabled]="loading" class="btn btn-primary">Search</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)="f.reset()">Reset</button>
</div>
</form>
这是我的组成部分:
export class HomeComponent implements OnInit {
// Represents the PowerPlantTypes
powerPlantTypes = ['RampUpType', 'OnOffType'];
// Represents the status of a PowerPlant
powerPlantStatuses = ['Active & Disabled', 'Active', 'Disabled'];
// Represents the search form
model: any = {};
// currentUser: User;
// represents the list of PowerPlant data
powerPlants: PowerPlant[];
users: User[] = [];
constructor(private powerPlantService: PowerPlantService) {
// this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
// Set the initial values for the drop down fields in the UI
}
ngOnInit() {
this.model.powerPlantType = '';
this.model.powerPlantStatus = '';
}
selectName() {
alert(this.model.powerPlantType);
}
searchPowerPlants(): void {
const powerPlantSearchParams = new PowerPlantSearchParams(
this.model.powerPlantType,
this.model.powerPlantOrg,
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;
});
}
}
当我点击html页面中的重置按钮时,方法searchPowerPlants()被调用。我怎么能避免这个?
编辑:使用type =“reset”后,它不再提交给服务器,但下拉列表显示其他值而不是默认值。从下面的屏幕截图中可以看出,下拉菜单现在在点击Reset按钮后将RampUpType显示为PowerPlantType,但我希望它显示--Select Type -
答案 0 :(得分:2)
在按钮上输入type =“button”并删除f.reset()
。代替:
<button type="button" (click)="reset()" class="btn btn-primary">Reset</button>
和
reset(){
this.model.powerPlantType = '';
this.model.powerPlantStatus = '';
}
您也可以从ngOnInit()调用reset()。 但是不要忘记为其他输入添加重置值。
https://stackblitz.com/edit/angular-zx3uwx?file=app%2Fapp.component.html