获取FormGroup里的FormControl对象,通过FormGroup对象的get("FormControlName 名")
示例:
在根模块导入
import { ReactiveFormsModule } from '@angular/forms'; @NgModule({ declarations: [ AppComponent, HeroFormComponent, FormcontrolComponent ], imports: [ BrowserModule, AppRoutingModule, FormsModule, ReactiveFormsModule ], providers: [], bootstrap: [AppComponent] }) html文件
public list:Array<String>=["北京","天津","深圳"] //创建 FormGroup对象 public fg:FormGroup=new FormGroup({ name:new FormControl(""), selectName:new FormControl("北京") }); constructor() { }
ngOnInit() { } //获取FormGroup对象里的FormControl对象 name=this.fg.get('name'); onSubmit(){ //获取FormGroup对象里的FormControl对象 const selected=this.fg.get('selectName'); //打印FormControl 对象的值 console.log(selected.value); //打印FormControl 对象的值 console.log(this.name.value);
}
}