简体   繁体   English

Angular6:基于下拉菜单中的选定值打开对话框

[英]Angular6: Open dialog based on selected value in dropdown

I have a p-dropdown with a list containing multiple options to select [options]="newUnitSelectItems" . 我有一个p-dropdown列表,其中包含一个包含多个选项的列表,以选择[options]="newUnitSelectItems" The goal is: if the user selects "other" from that list of options, a new dialog should open. 目标是:如果用户从该选项列表中选择“其他”,则应打开一个新对话框。 If the user selects any other value from the list, a simple console.log should be performed. 如果用户从列表中选择任何其他值,则应执行一个简单的console.log How do I achieve this? 我该如何实现?

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;
  }

I suppose the (onChange) is wrong here, because performConsoleLog() is called on every click. 我想这里的(onChange)是错误的,因为每次单击都会调用performConsoleLog() Is there a way to check which value is selected from [options]="newUnitSelectItems" in the html? 有没有办法检查从html的[options]="newUnitSelectItems"中选择了哪个值?

Thanks a lot! 非常感谢!

You can use the command in options of p-dropdown . 您可以在p-dropdown选项中使用该command

 newUnitSelectItems() {
    let selectItems: SelectItem[] = [
      {label: "%", value:"%"},
      {label: "g", value:"g"},
      {label: "other", value:"other", command :()=> this.openModal()} //<--command is here
    ];

   openModal(){
     console.log("Opening modal ");
   }

Remove onChange from select box. 从选择框中删除onChange

you can use $event of change event to get the value selected : 您可以使用change事件的$event获取选定的值:

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

in your TS : 在您的TS中:

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

Regards, 问候,

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

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