Angular 角度6:根据下拉列表中的选定值打开对话框

Angular 角度6:根据下拉列表中的选定值打开对话框,angular,select,dialog,dropdown,Angular,Select,Dialog,Dropdown,我有一个p下拉列表,其中包含多个选项可供选择[options]=“newUnitSelectItems”。目标是:如果用户从选项列表中选择“其他”,则应打开一个新对话框。如果用户从列表中选择任何其他值,则应执行简单的console.log。我如何做到这一点 new.component.html <p-dropdown name="newUnit" id="newUnit" [options]="newUnitSelectItems" [(ng

我有一个
p下拉列表
,其中包含多个选项可供选择
[options]=“newUnitSelectItems”
。目标是:如果用户从选项列表中选择“其他”,则应打开一个新对话框。如果用户从列表中选择任何其他值,则应执行简单的
console.log
。我如何做到这一点

new.component.html
      <p-dropdown name="newUnit" id="newUnit" [options]="newUnitSelectItems"
                  [(ngModel)]="Id"
                  (onChange)="performConsoleLog(Id, 'calc')"
                  ></p-dropdown>


new.component.ts
  performConsoleLog(unit: string, parameter: string) {
    if (parameter === 'calc') {
     console.log("No dialog open, unit: " + unit);
    }
  }

  newUnitSelectItems() {
    let selectItems: SelectItem[] = [
      {label: "%", value:"%"},
      {label: "g", value:"g"},
      {label: "other", value:"other"}
    ];
    return selectItems;
  }
new.component.html
new.component.ts
performConsoleLog(单位:字符串,参数:字符串){
如果(参数=='calc'){
console.log(“没有打开对话框,单位:+单位”);
}
}
newUnitSelectItems(){
让我们选择项目:选择项目[]=[
{标签:“%”,值:“%”,
{标签:“g”,值:“g”},
{标签:“其他”,值:“其他”}
];
返回选定的项目;
}
我想这里的
(onChange)
是错误的,因为每次单击都会调用
performConsoleLog()
。有没有办法检查从html中的
[options]=“newUnitSelectItems”
中选择的值

非常感谢

您可以在
p-下拉菜单的选项中使用
命令

newUnitSelectItems(){
让我们选择项目:选择项目[]=[
{标签:“%”,值:“%”,
{标签:“g”,值:“g”},
{标签:“其他”,值:“其他”,命令:()=>this.openModal()}//
您可以在
p-下拉菜单的选项中使用
命令

newUnitSelectItems(){
让我们选择项目:选择项目[]=[
{标签:“%”,值:“%”,
{标签:“g”,值:“g”},

{label:“other”,value:“other”,command:()=>this.openModal()}/您可以使用
$event
来获取所选的值:

<p-dropdown name="newUnit" id="newUnit" [options]="newUnitSelectItems"
                  [(ngModel)]="Id"
                  (onChange)="performConsoleLog($event)"
                  ></p-dropdown>

关于,

您可以使用
change
事件的
$event
来获取所选值:

<p-dropdown name="newUnit" id="newUnit" [options]="newUnitSelectItems"
                  [(ngModel)]="Id"
                  (onChange)="performConsoleLog($event)"
                  ></p-dropdown>
问候,

performConsoleLog($event) {
    if ($event.value.label === 'other') {
     // open your dialog.
    }
}