如何使用angular2将json数据设置为选择框 json数据 服务 ts文件 html Batch:选择批处理 {{item.batch}
问题 我想将如何使用angular2将json数据设置为选择框 json数据 服务 ts文件 html Batch:选择批处理 {{item.batch},angular,Angular,问题 我想将batch从json data设置为select box查看上面的json数据这来自JsonConvert.SerializeObject如何转换通过ngFor访问的普通数组 如何做?未测试的代码:您可以尝试这样做:首先像这样解析JSON: ts: 和视图: <option *ngFor="let item of getwithparamter">{{item.batch}}</option> 如前所述,这是没有测试,让我知道,如果任何一个工作 它工作得很好
batch
从json data
设置为select box
查看上面的json数据这来自JsonConvert.SerializeObject
如何转换通过ngFor
访问的普通数组
如何做?未测试的代码:您可以尝试这样做:首先像这样解析JSON: ts: 和视图:
<option *ngFor="let item of getwithparamter">{{item.batch}}</option>
如前所述,这是没有测试,让我知道,如果任何一个工作 它工作得很好,谢谢,但是相关的筛选函数不工作,显示错误,如原始异常:无法读取未定义的属性'filter',当我像{item.batch}一样尝试时,我觉得可能是在GetWithParameter检索数据之前进行筛选调用在variableYeah中检索数据之后调用筛选,这可能是问题所在——管道在检索数据之前尝试进行过滤。您可以尝试在那里添加一个
*ngIf
,它可能会解决这个问题:
getwithParamter()
{
let params = {program: 'pravin'};
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json; charset=utf-8');
//this.headers.append('Parameter', params);
let options = new RequestOptions({
method: RequestMethod.Post,
url: "AttendanceMast.aspx/getBatch",
headers: this.headers,
body:{program: 'IFDM',location:'Pune',createdby:'ifdmpune'}
});
return this._http.request(new Request(options)).retry(2)
.map((response: Response) => JSON.parse(response.text()))
.do(data => console.log('All: ' + data))
.catch(this.handleError);
}
this.dataService.getwithParamter().subscribe(
tradeshows => this.getwithparamter = tradeshows,
error => console.error('Error: ' + error)
);
Batch : <select [(ngModel)]="sel_batch" > <option >Select Batch</option>
<option *ngFor="let item of getwithparamter ">{{item.batch}}</option>
something: any;
this.dataService.getwithParamter().subscribe(
tradeshows => {
this.something = tradeshows
this.getwithparameter = JSON.parse(this.something.d)
});
<option *ngFor="let item of getwithparamter">{{item.batch}}</option>
<option *ngFor="let item of getwithparamter.d">{{item.batch}}</option>
this.dataService.getwithParamter().subscribe(
tradeshows => {
this.getwithparameter = tradeshows
});