简体   繁体   English

使用 <p-dropdown> 带表格控制

[英]Using <p-dropdown> with form control

I think I am having an issue with value binding. 我认为我在价值约束方面存在问题。 I have 2 dropdowns on my page currently. 我的页面上目前有2个下拉菜单。 The rest of the page is using PrimeNg for UI and would like to make these dropdowns look the same as the rest of the page. 页面的其余部分正在使用PrimeNg for UI,并希望使这些下拉菜单与页面的其余部分相同。 How should I go about making this work. 我应该如何进行这项工作。

One dropdown is a supervisor list. 一个下拉列表是一个主管列表。

<div class="ui-g form-group">
  <label for="supervisors">Supervisors * </label>
  <select 
    class="form-control"
    id="supervisors" 
    required
    [(ngModel)]="model.supervisor"
    name="supervisor"
  >
    <option *ngFor="let sup of supervisors" [value]="sup">
      {{sup}}
    </option>
    <div
      [hidden]="supervisors.valid || supervisors.pristine"
      class="alert alert-danger"
    >
      Supervisor is required
    </div>
  </select>
</div>

The other is a leave code list 另一个是请假代码列表

<div class="ui-g-12 ui-md-1" id="test">
   <label for="codes">Leave Codes * </label>
   <select 
     class="form-control"
     id="codes"
     placeholder="Select Leave Code *"
     required 
     [(ngModel)]="model.code" 
     name="code"
    >
      <option *ngFor="let cod of codes" [value]="cod">{{cod}}</option>
    </select>
</div>

I have 2 arrays of values being called from my .ts file 我的.ts文件中有2个值数组

supervisors = ['Alex',"Jones",'Joe','Rogan'];
codes = ['Personal Leave','Vacation Leave', 'Sick Leave'];

When I use the tags I just get an empty drop down. 使用标签时,我会得到一个空的下拉列表。 I tried just using initially but then I was not able to get the required fields to validate. 我最初只是尝试使用,但是后来我无法获得必填字段以进行验证。

Did you import DropdownModule? 您导入了DropdownModule吗?

import {DropdownModule} from 'primeng/dropdown';

See the documentation , html binding should be 参见文档 ,html绑定应该是

<p-dropdown [options]="supervisorList" [(ngModel)]="supervisor"></p-dropdown>

where supervisorList will be defined as SelectItem in controller and needs to be in a label + value format. 其中supervisorList将在控制器中定义为SelectItem ,并且需要采用标签+值格式。

supervisorList: SelectItem[];
this.supervisorList= [
            {label: 'Alex', value: 'Alex'},
            ...
];

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

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