【发布时间】:2022-11-28 12:23:40
【问题描述】:
我将数据从父组件传递到子组件,并且填充了从父组件接收到的数据的普通文本输入,但这不适用于下拉列表。
我将数据传递给变量的代码块输入名称就像我希望的那样工作。
<span class="p-float-label">
<input #nameID="ngModel" [(ngModel)]="enteredName" [style]="{'width':'100%'}" [textContent]="name" id="nameID"
maxlength="256" name="nameID" pInputText required type="text" />
<label for="nameID">Name</label>
</span>
但是当我通过“选定的记录器类型" 它不会显示为从下拉列表中选择的选项。
下面是它的代码。
<span class="p-float-label">
<p-dropdown [options]="recorderType" name="recorderTypeID" id="recorderTypeID" optionLabel="name"
[autoDisplayFirst]="false" #recorderTypeID="ngModel" [(ngModel)]="selectedRecoderType"
[disabled]="(!userCanAdd && !userCanModify)" (onChange)="onChangeRecorderType($event)" [required]=true [style]="{'width': '100%'}" appendTo="body">
</p-dropdown>
<label for="recorderTypeID">Recorder Type</label>
</span>
下面是组件 TS 文件 我将变量从“选定的录音机“ 至 ”selectedRecoderType",但后来我仍然遇到同样的问题。 我能知道我做错了什么吗?
P.S 我也更新了下面的截图。
this.enteredName = "hey";
this.selectedRecoderType = "Hello";
编辑
this.enteredName = "嘿"; this.selectedRecoderType = "你好";
【问题讨论】:
-
在模板中你有
selectedRecorderType但你的 TS 显示selectedRecorder,不一样。 -
此外,“Hello”需要存在于选项列表(
recorderType)中,p-dropdown会自动将其显示为选定值。 -
@Patrick 和我已经进行了更改并编辑了问题,但我仍然无法弄清楚我犯了什么错误。
-
@Nehal 我已经进行了更改并编辑了问题,但我仍然无法弄清楚我犯了什么错误
-
您仍然没有解决
recorderType中的内容,这些内容作为选项传递给p-dropdown-><p-dropdown [options]="recorderType" ...