[英]Angular 6: unable to retrieve all form Control value in HTML using {{form.value | json}}
I am new to Angular trying with version 6. 我是Angular尝试第6版的新手。
Requirement I have two controls (radio and dropdown) and i want to print all controls value in json in HTML form only using {{form.value | 要求我有两个控件(无线电和下拉列表),我想用HTML格式打印json中的所有控件值只使用{{form.value | json}} JSON}}
Problem {{form.value | 问题 {{form.value | json}} is printing only radio button value but not printing dropdownlist selected value. json}}仅打印单选按钮值,但不打印下拉列表选定值。 Below is my code snippet, please help me out- 以下是我的代码片段,请帮助我 -
//app.component.ts import { Component,OnInit } from '@angular/core'; import {FormBuilder,FormGroup,Validators} from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.styl'] }) export class AppComponent implements OnInit { title = 'AngularRadioDropDownCheckBox'; genders:string[]; communicationMode:string[]; genderForm:FormGroup; constructor(private formBuilder:FormBuilder){ this.genderForm=this.formBuilder.group({ gender:[], communication:[] }) } ngOnInit(){ this.genders=["male","female","others"]; this.communicationMode=["mobile","telephone","email"]; } }
--AppComponent.html <!--The content below is only a placeholder and can be replaced.--> <form [formGroup]="genderForm" #radioForm="ngForm"> <div class="radio"> <label for="gender" *ngFor="let gender of genders"> <input type="radio" formControlName="gender" name="gender" value={{gender}} ngModel >{{gender}} </label> </div> <div class="form-group"> <select formControleName="communication" name="commMod" > <option *ngFor="let commMod of communicationMode" value={{commMod}} ngModel >{{commMod}}</option> </select> </div> {{genderForm.value | json}} </form> <router-outlet></router-outlet>
//app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import {ReactiveFormsModule,FormsModule} from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, ReactiveFormsModule, FormsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Try this way 试试这种方式
<select (change)="change($event.target.value)" name="commMod" >
<option *ngFor="let commMod of communicationMode">{{commMod}}</option>
</select>
{{genderForm.value | json}}
{{communication}}
.ts file .ts文件
communication:any = '';
change(event){
this.communication = event;
}
You need to get the dropdown click event and bind it to model. 您需要获取下拉单击事件并将其绑定到模型。 with the help of change()
you will get selected dropdown value. 在change()
的帮助下,您将获得选定的下拉值。
You are having 2 mistakes 你有2个错误
1) formControleName should be formControlName (typo mistake) 1)formControleName应该是formControlName(错字错误)
2) you should not use ngmodel inside select tag as it will give you a console errors. 2)你不应该在select标签内使用ngmodel,因为它会给你一个控制台错误。
like this 像这样
<select formControleName="communication" name="commMod" >
<option *ngFor="let commMod of communicationMode" value={{commMod}} >{{commMod}}</option>
</select>
TS TS
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app',
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {
title = 'AngularRadioDropDownCheckBox';
genders: string[];
communicationMode: string[];
genderForm: FormGroup;
constructor() {
this.genderForm = new FormGroup({
'gender': new FormControl('male'),
'communication': new FormControl(null)
});
}
ngOnInit() {
this.genders = ["male", "female", "others"];
this.communicationMode = ["mobile", "telephone", "email"];
}
}
HTML HTML
<form [formGroup]="genderForm" #radioForm="ngForm">
<div class="radio">
<label for="gender" *ngFor="let gender of genders">
<input type="radio" formControlName="gender" name="gender" [value]="gender">{{gender}}
</label>
</div>
<div class="form-group">
<select formControlName="communication" name="commMod">
<option *ngFor="let commMod of communicationMode" [value]="commMod" >{{commMod}}</option>
</select>
</div>
{{genderForm.value | json}}
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.