简体   繁体   English

将数据从API绑定到Angular 7中的Dropdown

[英]Bind data from API to Dropdown in Angular 7

I'm trying to populate a dropdown from a get API. 我正在尝试从get API填充下拉列表。 I've created a service and using the observables returning the data to the component, which then subscribes to the service. 我创建了一个服务,并使用可观察对象将数据返回到组件,该组件随后订阅了该服务。 I'm able to console.log the whole data (which is in a JSON array) but I'm not able to see any data in the dropdown. 我可以console.log整个数据(在JSON数组中),但是在下拉列表中看不到任何数据。

I guess the problem is that view is getting rendered before the API data. 我想问题是视图是在API数据之前呈现的。 I know there is a resolve method but what else I can try? 我知道有解决方法,但是我还能尝试什么?

My question and code are very much similar to the below question and there are few things which I've already correct in my code like 'Project' is an array but the solution isn't working for me. 我的问题和代码与以下问题非常相似,并且在我的代码中我已经纠正了一些问题,例如“ Project”是一个数组,但是解决方案不适用于我。 There is no error in my code also: 我的代码也没有错误:

Drop down does not populate with API data in Angular 下拉列表中未填充API数据

Please find my code below: TestService.ts 请在下面找到我的代码: TestService.ts

export class TestService {
  public testURL = 'https://test.url';
  constructor(private _http: HttpClient) { }

    getTypes(): Observable<TestModel[]> {
    const headers = new HttpHeaders({'test': 'test1'});
    return this._http.get<TestModel[]>(this.testURL, {headers});
  }
  }

Component.ts Component.ts

 @Component({
  selector: 'app-header',
  templateUrl: './app-header.component.html',
  styleUrls: ['./app-header.component.css']
})
export class AppHeaderComponent implements OnInit, AfterViewInit {
   testModel: TestModel[];
   isLoaded = false;

  constructor(private _testService: TestService) { }

  getTypeT(): void {
    this._testService.getTypes().subscribe(data => {
      if(data) {
        this.testModel = data;
        this.isLoaded = true;
        console.log(this.testModel);
      }
    } );
  }

  ngOnInit() {
    // fetch all the survey types
    this.getTypeT();
  }

component.html component.html

  <select  class="selectpicker dropdown mt-4 ml-3" id="type" data-live-search="true">
      <option *ngFor="let test of testModel" [value]="test.id">{{test.description}}</option>
   </select>

Note : I just figured out that problem seems to be with the " selectpicker " dropdown which is a third party plugin. 注意 :我刚刚发现问题似乎出在第三方插件“ selectpicker ”下拉列表中。 Could you please suggest what I can do to resolve it? 您能否提出我可以解决的建议?

You could try using the async pipe. 您可以尝试使用异步管道。

@Component({
  selector: 'app-header',
  templateUrl: './app-header.component.html',
  styleUrls: ['./app-header.component.css']
})
export class AppHeaderComponent implements OnInit, AfterViewInit {
   testModel: TestModel[];
   isLoaded = false;
   types$;

  constructor(private _testService: TestService) { }

  getTypeT(): void {
    return this._testService.getTypes();
  }

  ngOnInit() {
    this.types$ = this.getTypeT();
  }

HTML 的HTML

  <select  class="selectpicker dropdown mt-4 ml-3" id="type" data-live-search="true">
      <option *ngFor="let test of types$ | async" [value]="test.id">{{test.description}}</option>
   </select>

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

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