html - 如何将多个输入字段的预选值映射到角度6的数组中
问题描述
我有一些输入字段,我需要在提交时将其预选值捕获为数组格式,如 {"userid":1,"newstatus":[1],"mygroup":[1,2,3]}尝试使用 ngmodel 但它无法正常工作,这是下面的代码
home.component.html
<div class="form-group">
<label for="email">Email address:</label>
<input type="text" class="form-control" [(ngModel)]="userid" id="userid">
</div>
<div class="form-group">
<label for="pwd">List:</label>
<select (change)="onSelectChange($event)" [(ngModel)]="newstatus" class="form-control" id="list">
<option *ngFor="let status of statusdata" value="{{status.id}}">{{status.name}}</option>
</select>
</div>
<div class="form-group" *ngFor="let status of statusdata" >
<input type="checkbox" [(ngModel)]="mygroup" (input)="onCheckChange($event)" value="{{status.id}}"/>{{status.name}}
</div>
<button type="submit" (click)="getFormData()" class="btn btn-default">Submit</button>
home.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
getListData: any;
dataGroup: FormGroup;
selectedGroups: string[];
selectedRoles:string[];
statusdata:any;
userid: number;
newstatus:any;
mygroup:any;
constructor() {
this.selectedGroups = [];
this.selectedRoles = [];
}
ngOnInit() {
this.statusdata = [{"id":1,"name":"green"},{"id":2,"name":"red"},{"id":3,"name":"yellow"}];
}
getFormData(){
console.log(this.userid)
console.log(this.newstatus)
console.log(this.mygroup)
}
}
解决方案
一种实现方式如下:
statusdata
在数组中添加了一个字段,选中。根据此属性的值,获取选定的值。检查代码以获取更多信息。
home.component.html
<div class="form-group">
<label for="email">Email address:</label>
<input type="text" class="form-control" [(ngModel)]="userid" id="userid">
</div>
<div class="form-group">
<label for="pwd">List:</label>
<select (change)="onSelectChange($event)" [(ngModel)]="newstatus" class="form-control" id="list">
<option *ngFor="let status of statusdata" value="{{status.id}}">{{status.name}}</option>
</select>
</div>
<div class="form-group" *ngFor="let status of statusdata" >
<input type="checkbox" [(ngModel)]="status.selected"/>{{status.name}}
</div>
<button type="submit" (click)="getFormData()" class="btn btn-default">Submit</button>
home.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
getListData: any;
dataGroup: FormGroup;
selectedGroups: string[];
selectedRoles:string[];
statusdata:any;
userid: number;
newstatus:any;
mygroup:any;
constructor() {
this.selectedGroups = [];
this.selectedRoles = [];
}
ngOnInit() {
this.statusdata = [{"id":1,"name":"green", "selected":true},{"id":2,"name":"red", "selected":false},{"id":3,"name":"yellow", "selected":true}];
}
getFormData(){
//alert(this.userid)
//alert(this.newstatus)
//alert(this.mygroup)
let _this = this;
_this.selectedGroups = [];
_this.selectedRoles = [];
_this.selectedRoles.push(this.newstatus);
this.statusdata.forEach(function(item){
if(item.selected == true)
_this.selectedGroups.push(item.id);
});
let result = {};
result.userid = _this.userid;
result.newstatus = _this.selectedRoles;
result.mygroup = _this.selectedGroups;
console.log(result);
}
}
推荐阅读
- node.js - 我需要一个 NodeJs 开源服务器来下载并在 Windows NodeJS 服务器上运行
- powershell - Param with more values
- java - Java 并发:CopyOnWriteArrayList 行为
- swift - 是否可以通过 ARKit 的图像识别来检测两次或多次相同的图像?
- powershell - 将变量添加到 Invoke-RestMethod
- python - Pandas:如何将列(坏数据)中的太大值设置为零,我应该使用 if 函数还是完全不同的东西?
- angularjs - Angularjs 引导选项卡
- javascript - 遍历这个结构?
- java - java中条件为真的while循环
- docker - 使用域名从主机网络访问 Docker