【问题标题】:How to pop up a modal from click in dropdown list option如何通过单击下拉列表选项弹出模式
【发布时间】:2020-05-25 12:19:46
【问题描述】:

开发人员好日子,我正在使用 angular 使用这个应用程序,现在我正在尝试一次选项之一 被点击,显示一个模态标签。 基本上我所做的是创建一个与下拉列表中选择的项目相等的并行模板,并在此模板上使用 a 标签设置所有逻辑以显示模态,但猜测不是用户友好的原因,导致了几次额外的点击。尝试在选项中设置 a 标签也不可行,因为我的下拉菜单不起作用。这里模拟一下我所做的:

HTML tag

      <select [hidden]="!state" name="optionsInc" required [(ngModel)]="optionsInc" (change)="subItemSelected($event)">
        <option value="select" [ngValue]="null" [disabled]="true">Select Income</option>
        <option *ngFor="let item of allKeysIncomings" label="{{item}}" value="{{item}}"></option>
      </select>====>DROPDOWN LIST LOGIC


    <p [hidden]="!state"> <a *ngIf="incomeSelected"
      href="#"
      class="btn btn-primary btn-block"
      data-toggle="modal"
      data-target="#editItem"
    >{{incomeSelected}}</a>
    </p>====>PARALELL REFERENCE TO POP THE MODAL UP

    <div class="modal fade" id='editItem'>======>MODAL 
      SOME TAGS AND CODE
    </div>

然后在我的组件上我这样做了:

imports...

@Component({
  selector: 'app-user-sheet-balance',
  templateUrl: './user-sheet-balance.component.html',
  styleUrls: ['./user-sheet-balance.component.css'],
})
export class UserSheetBalanceComponent implements OnInit {

allKeysIncomings: any;==>ITERABLE
incomeSelected: string;

constructor(some code) {}

ngOnInit(): void {some code}

  async subItemSelected(event) {
    SOME CODE
      return (
        await (this.incomeSelected = event.target.value),
     );
  }

一旦我单击标签 a,所有这些过程都会执行激活模式的任务,但不是创建对下拉列表的并行引用,我想知道实际上是否可以直接从下拉列表中执行此操作。 我一直在社区上看到一些类似的问题,例如 :Open a Modal Using an Option from a Dropdown - Angular 2 + ngx 但不适用于我的代码规范。 对此有任何更新的想法吗?提前谢谢!!!!!!

【问题讨论】:

    标签: javascript angular drop-down-menu


    【解决方案1】:

    如果您在ModalComponent 中有带有对话框布局的Component,它应该如下工作

        import { Injectable } from '@angular/core';
        import { MatDialog, MatDialogRef } from '@angular/material/dialog';
        import { ModalComponent } from './modal/modal.component';
    
        @Injectable({
          providedIn: 'root'
        })
        export class TestDialogService {
    
          dialogRef: MatDialogRef<ModalComponent, any>;
    
          constructor(public dialog: MatDialog) { }
    
          open() {
            if(this.dialogRef) {
              this.dialogRef.close();
            }
            this.dialogRef = this.dialog.open(ModalComponent, {
              panelClass: 'app-dialog'
            });
          }
    
          close() {
            if(this.dialogRef) {
              this.dialogRef.close();
            }
          }
        }
    
        // html
        <mat-form-field>
          <mat-label>Favorite car</mat-label>
          <select name="optionsInc"
            matNativeControl 
            [(ngModel)]="optionsInc" 
            (ngModelChange)="onModelChange()">
    
            <option value="select" [value]="null" [disabled]="true">Select Income</option>
            <option *ngFor="let item of allKeysIncomings" [label]="item.viewValue" 
              [value]="item.value"></option>
          </select>
        </mat-form-field>
    
        // ts
        @Component({
          selector: 'app-root',
          templateUrl: "./app.component.html",
          styleUrls: ["./app.component.scss"]
        })
        export class AppComponent {
          state = false;
          optionsInc = null;
          allKeysIncomings = [
            {value: 'volvo', viewValue: 'Volvo'},
            {value: 'saab', viewValue: 'Saab'},
            {value: 'mercedes', viewValue: 'Mercedes'}
          ];
    
          constructor(
            public testDialogService: TestDialogService) {
          }
    
          onModelChange() {
            this.testDialogService.open();
          }
        }
    
    

    example

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多