簡體   English   中英

Primeng p-dropdown指令不顯示所選值

[英]Primeng p-dropdown directive not displaying selected value

我正在使用primeng的下拉指令允許用戶為進入日歷應用程序的事件選擇約會類型。 下拉列表的向下箭頭顯示,單擊它時,用戶可以正常選擇列表中的項目,但下拉列表中沒有顯示所選項目的顯示區域。 我可以驗證選擇項目是否有效,然后單擊某個事件,然后單擊下拉箭頭將顯示所選的正確項目。 下面是我的組件的ts,html和css文件。

在此輸入圖像描述

預約-detail.component.ts:

import { Input, Component, Output, EventEmitter } from '@angular/core';
import { Event } from '../../shared'
import { Calendar, Button, Dropdown, SelectItem } from 'primeng/primeng'

@Component({
  moduleId: module.id,
  selector: 'appointment-detail',
  templateUrl: 'appointment-detail.component.html',
  styleUrls: ['appointment-detail.component.css'],
  directives: [Calendar, Button, Dropdown]
})
export class AppointmentDetailComponent {
  @Input() event;
  @Output() submitDetails: EventEmitter<any> = new EventEmitter();
  appointmentTypes: SelectItem[];

  constructor() {
    this.event = new Event();
    this.appointmentTypes = [];
    this.appointmentTypes.push({label: 'Select type:', value: ''})
    this.appointmentTypes.push({label: 'Meeting', value: 'Meeting'})
    this.appointmentTypes.push({label: 'Vacation', value: 'Vacation'})
    this.appointmentTypes.push({label: 'Other', value: 'Other'})
  }

  saveEvent() {
    this.submitDetails.emit({action: 'save', event: this.event});
  }

  deleteEvent() {
    this.submitDetails.emit({action: 'delete', event: this.event});
  }
}

預約-detail.component.html:

<div class="modal-content" *ngIf="!event">Loading...</div>
<div class="modal-content" *ngIf="event">
  <h2 class="header"> Appointment Details </h2>
  <div class="ui-grid ui-grid-responsive ui-fluid">
    <div class="ui-grid-row">
      <div class="ui-grid-col-2"></div>
      <div class="ui-grid-col-2"><label for="vin">Title</label></div>
      <input class="ui-grid-col-6" pInputText id="title" [(ngModel)]="event.title" />
    </div>
    <div class="ui-grid-row">
      <div class="ui-grid-col-2"></div>
      <div class="ui-grid-col-2"><label for="start">Start</label></div>
      <div class="ui-grid-col-6">
        <p-calendar id="start" dateFormat="yy-mm-dd" [(ngModel)]="event.start"></p-calendar>
      </div>
    </div>
    <div class="ui-grid-row">
      <div class="ui-grid-col-2"></div>
      <div class="ui-grid-col-2"><label for="end">End</label></div>
      <div class="ui-grid-col-6">
        <p-calendar id="end" dateFormat="yy-mm-dd" [(ngModel)]="event.end" placeholder="Optional"></p-calendar>
      </div>
    </div>
    <div class="ui-grid-row">
      <div class="ui-grid-col-2"></div>
      <div class="ui-grid-col-2"><label for="location">Location</label></div>
      <input class="ui-grid-col-6" id="location" [(ngModel)]="event.location" />
    </div>
    <div class="ui-grid-row">
      <div class="ui-grid-col-2"></div>
      <div class="ui-grid-col-2"><label for="type">Type</label></div>
      <p-dropdown class="ui-grid-col-6" id="type" [options]="appointmentTypes" [(ngModel)]="event.type"></p-dropdown>
    </div>
    <div class="ui-grid-row">
      <div class="ui-grid-col-2"></div>
      <div class="ui-grid-col-2"><label for="details">Details</label></div>
      <input class="ui-grid-col-6" pInputText id="details" [(ngModel)]="event.details" />
    </div>
  </div>
  <footer>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
      <span class="footer-padding"></span>
      <button class="footer-button" type="button" pButton icon="fa-check" (click)="saveEvent()" label="Save"></button>
      <button class="footer-button" type="button" pButton icon="fa-close" (click)="deleteEvent()" label="Delete"></button>
      <span class="footer-padding"></span>
    </div>
  </footer>
</div>

預約-detail.component.css:

.header {
    text-align: center;
    margin: 15px;
}

.ui-grid-row {
    margin-bottom: 4px;
    margin-top: 4px;
}

.ui-dialog-buttonpane {
    padding: 3px;
    display: flex;
}

.footer-padding {
    flex: 4;
}

.footer-button {
    flex: 2;
}

您的錯誤在此行中:

this.appointmentTypes.push({label: 'Select type:', value: ''})

如果要顯示下拉列表選定值,則默認選項的值必須為null:

this.appointmentTypes.push({label: 'Select type:', value: null})

在你的下拉列表中添加appendTo="body"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM