简体   繁体   中英

How to use Datalist with dynamic json in angular 8

I am using datalist whose options are coming from dynamic json. I am also using value in the options which is also coming with the selection list of dropdown. How to remove that value from selection,can anyone please help me. Here is the code below.

home.component.html

<div>
<input type="text"  list="codes">
<datalist id="codes">
  <option *ngFor="let c of statusdata" [value]="c.id" >{{c.name}}</option>
</datalist>
</div>

home.component.ts

import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
declare var $: any;
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  statusdata: any;

  ngOnInit() {

      this.statusdata = [{ id: 1, name: 'Angular 2+' },
    { id: 2, name: 'Angular 4' },
    { id: 3, name: 'Angular 5' },
    { id: 4, name: 'Angular 6' },
    { id: 5, name: 'Angular 7' }
  ];
  }
}

the problem is because datalist should have only value like this example

<input list="data" name="data">
<datalist id="data">
  <option value="data01">
  <option value="data02">
</datalist>

So in your case should be

<option *ngFor="let c of statusdata" [value]="c.name">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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