简体   繁体   English

Angular 4在HTML页面中使用ndFor和Ngif显示Json数组值

[英]Angular 4 display Json array value using ndFor and Ngif in HTML page

I have an HTMLform page where I am trying to show an array of Json data in seperate text boxes,but I am unable to do so.My component level code is below: 我有一个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']);  
}

My JSON data is as follows: 我的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}

I want to show tool owner and business owner as separate text boxes with their corresponding contact_Name values for that array index and also if comma separated values are present in contact_name ,how should I go and show that in the text box. 我想将工具所有者和业务所有者显示为单独的文本框,并带有该数组索引的相应contact_Name值,并且如果contact_name中存在逗号分隔的值,我该如何在文本框中显示它。 I am stuck in this for long time,kindly please help me with this. 我被困在这里很长时间了,请帮助我。 My HTML code is below where I am trying with ngfor to iterate through the array and want to check if tool owner and busines owner has value then display respective contact_name in separate text boxes . 我的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>

Try this. 尝试这个。 I think it will be what you want. 我认为这将是您想要的。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM