我有一个HTMLform页面,我试图在单独的文本框中显示一个Json数据数组,但我无法这样做。我的组件级代码如下:
import { Component, OnInit } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { FormsModule,NgForm, FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { CatalogComponent } from '../catalog/catalog.component';
import { CatalogService } from '../services/Catalog.service';
import { ContactService } from '../services/Contact.service';
import { URLService } from '../services/URL.service';
import { SupportService } from '../services/Support.service';
import { Catalog } from '../classes/Catalog';
import { Contact } from '../classes/Contact';
import { URL } from '../classes/URL';
import { Support } from '../classes/Support';
import { Category } from '../classes/Category';
@Component({
selector: 'app-catalog-form',
templateUrl: './catalog-form.component.html',
styleUrls: ['./catalog-form.component.scss']
})
export class CatalogFormComponent implements OnInit {
Catalogdata:Catalog;
Contacts:Contact[];
URLs:URL[];
Supportdata:Support[];
CatalogForm:FormGroup;
title: string = "";
id: number;
errorMessage: any;
constructor( private _fb: FormBuilder,private _avRoute: ActivatedRoute,
private catService: CatalogService,private conservice:ContactService,
private urlservice:URLService,private supservice:SupportService, private _router: Router) {
if (this._avRoute.snapshot.params["id"]) {
this.id = this._avRoute.snapshot.params["id"];
}
this.CatalogForm = this._fb.group({
Category: [''],
SubCategory:[''],
ItemName:[''],
Description:[''],
IAP_Number:[''],
/*ToolOwner:[''],
BusinessOwner:[''],
ProdURL:[''],
IncidentURL:[''],
RequestURL:[''],
SupportType:[''],
SupportValue:[''],
SupportLink:['']*/
})
}
ngOnInit() {
console.log("I am in form component",this.id);
if (this.id > 0) {
this.title = "View";
console.log("Title of form:",this.title) ;
this.catService.getCatalogDetails(this.id)
.subscribe( t =>
this.CatalogForm.patchValue
({Category:t,SubCategory:t,Description:t,ItemName:t,IAP_Number:t}) , err =>
console.log("Error messgae:",this.errorMessage)
);
}
console.log("Catalog and Category Details:",this.CatalogForm);
//.controls.Category.
//get(['cItem']));
}
cancel() {
this._router.navigate(['/home']);
}
我的JSON数据如下:
Array(2)
0
:
{contact_Id: 5, catalog_Item_Id: 4, contact_Type: "Tool Owner", contact_Name: "tyu", catalog_Item_ID: null}
1
:
{contact_Id: 6, catalog_Item_Id: 4, contact_Type: "Business Owner", contact_Name: "BC", catalog_Item_ID: null}
我想将工具所有者和业务所有者显示为具有该数组索引的相应contact_Name值的单独文本框,并且如果contact_name中存在逗号分隔值,我应该如何在文本框中显示该值。我被困在这里很长一段时间,请帮助我。 我的HTML代码位于我尝试使用ngfor迭代数组并想要检查工具所有者和业务所有者是否有值然后在单独的文本框中显示相应的contact_name。
<fieldset>
<legend>About Tool</legend>
<form [formGroup]="CatalogForm" #formDir="ngForm" novalidate>
<div class="form-group row">
<!-- <div *ngIf="ItemName != null"></div>-->
<!--<div *ngFor="let c of CatalogForm;let i=index"></div>-->
<label class="control-label col-md-12">ItemName</label>
<div class="col-md-4"></div>
<input class="form-control" readonly="true" type="text" formControlName="Category" >
</form>
答案 0 :(得分:0)
试试这个。我想这将是你想要的。
<div *ngFor="let c of CatalogForm ">
<div *ngIf="c === 'Tool Owner'">
{{c.contact_Type}} ..........
</div>
</div>
<div *ngFor="let c of CatalogForm ">
<div *ngIf="c === 'Business Owner'">
{{c.contact_Type}}. ..........
</div>
</div>