下拉菜单不会填充

下拉菜单不会填充

本文介绍了下拉菜单不会填充 Angular 中的 API 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 api 检索数据到下拉列表,但我不知道我的代码有什么问题.

I want to retrive data to dropdown from api and I don't know what is wrong in my code.

API结果:

[{"projectId":2,"projectName":"test","gates":[]},{"projectId":3,"projectName":"project1","gates":[]}]

服务模型(project.ts):

export class Project{
    projectName: string;
    projectId :number;
}

我的 sidebar.component.html 中的下拉菜单:

Dropdown in my sidebar.component.html:

<div class="dropdown  ml-auto">
    <select (change)=selectedHandlerProjectName($event) class="btn btn-secondary dropdown-toggle" style="margin-left: -10px;border-left-width:15px;padding-left:11px;padding-right: 14px;margin-right: 12px;border-right-width: 12px">
        <option value = "default">Select project</option>
        <option *ngFor = "let projectName of selectProject?.projectName" value = {{projectName}}>{{projectName}}</option>
    </select>
</div>

Sidebar.component.ts

import { Component, OnInit } from '@angular/core';
import { Project } from '../models/Project';
import {ProjectService} from '../services/project.service';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {

  selectProject : Project;
  constructor(private projectService: ProjectService) { }

  getSelectedProject(): void{
    this.projectService.getProjects()
      .subscribe(selectProject => this.selectProject= selectProject);
  }
  //methods to get dropdown values
  dropDownProjectName: string = '';
  selectedHandlerProjectName(event : any)
  {
    if(event.target.value != 'default') { this.dropDownProjectName = event.target.value;}
    else {this.dropDownProjectName = null;}
  }

  ngOnInit() {
    this.getSelectedProject();

  }
}

Project.service.ts

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import {catchError,map,tap} from 'rxjs/operators';
import { Project } from '../models/Project';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class ProjectService {

  private urlProject = environment.baseUrl + environment.urlProject ;

  constructor(private http: HttpClient) { }

  getProjects(): Observable<any>{
    return this.http.get<Project>(this.urlProject)
    .pipe(
    catchError(this.handleError('getProject',[]))
  );
  }
}

如果我在应用程序运行时查看网络,会出现来自 API 的数据,但不会出现在下拉列表中.

If i look in Network when app running , appear data from API, but in dropdown not.

推荐答案

试着用你的 html 下拉代码替换它.它会起作用的.因为你不需要在*ngFor中检查productName*ngFor = "let projectName of selectProject?.projectName"

just try to replace it with your html dropdown code. it will works. because you don't need to check productName in *ngFor, *ngFor = "let projectName of selectProject?.projectName"

<select class="btn btn-secondary dropdown-toggle" >
    <option value="default">Select project</option>
    <option *ngFor="let project of selectProject" [value]="project.projectName">{{project.projectName}}</option>
</select>

这篇关于下拉菜单不会填充 Angular 中的 API 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:29