【问题标题】:How handle a button in typescript in dynamic code如何在动态代码中处理打字稿中的按钮
【发布时间】:2019-08-14 07:04:10
【问题描述】:

我正在使用Smart Admin Angular Version,当我想从 notificationService 使用时,我遇到了问题。

我无法处理 notificationService.smallBox 中的按下按钮。

我的代码如下:

notificationExample5() {

  this.notificationService.smallBox({
    title: "Ding Dong!",
    content: "Someone's at the door...shall one get it sir? <p class='text-align-right'><a href-void class='btn btn-primary btn-sm'>Yes</a> <a href-void class='btn btn-danger btn-sm'>No</a></p>",
    color: "#296191",
    //timeout: 8000,
    icon: "fa fa-bell swing animated"
  });
}

如何处理 Angular 中的“是”或“否”按钮?

【问题讨论】:

    标签: angular typescript smartadmin


    【解决方案1】:

    你可以有多种方法来实现它。

    尝试https://stackoverflow.com/a/43065100/8179245已经回答的解决方案。

    或者使用为你创建的这个 stackbliz https://stackblitz.com/edit/angular-confirmation-dialog-1i8zgw?file=app/app.component.ts

    应用组件 Ts

    import { Component } from '@angular/core';
    
    import { ConfirmationDialogService } from './confirmation-dialog/confirmation-dialog.service';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      constructor(private confirmationDialogService: ConfirmationDialogService) {}
    
      public openConfirmationDialog() {
        this.confirmationDialogService.confirm('Please confirm..', 'Do you really want to ... ?')
        .then((confirmed) => console.log('User confirmed:', confirmed))
        .catch(() => console.log('User dismissed the dialog (e.g., by using ESC, clicking the cross icon, or clicking outside the dialog)'));
      }
    }
    

    应用组件 HTML

    <button (click)="openConfirmationDialog()" type="button" class="btn btn-primary">Open dialog</button>
    
    <p>Open the console to see log statements.</p>
    

    确认框 TS

    import { Component, Input, OnInit } from '@angular/core';
    import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
    
    @Component({
      selector: 'app-confirmation-dialog',
      templateUrl: './confirmation-dialog.component.html',
    })
    export class ConfirmationDialogComponent implements OnInit {
    
      @Input() title: string;
      @Input() message: string;
      @Input() btnOkText: string;
      @Input() btnCancelText: string;
    
      constructor(private activeModal: NgbActiveModal) { }
    
      ngOnInit() {
      }
    
      public decline() {
        this.activeModal.close(false);
      }
    
      public accept() {
        this.activeModal.close(true);
      }
    
      public dismiss() {
        this.activeModal.dismiss();
      }
    
    }
    

    确认框 HTML

    <div class="modal-header">
      <h4 class="modal-title">{{ title }}</h4>
        <button type="button" class="close" aria-label="Close" (click)="dismiss()">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        {{ message }}
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" (click)="decline()">{{ btnCancelText }}</button>
        <button type="button" class="btn btn-primary" (click)="accept()">{{ btnOkText }}</button>
      </div>
    

    确认收件箱服务

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    
    import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
    
    import { ConfirmationDialogComponent } from './confirmation-dialog.component';
    
    @Injectable()
    export class ConfirmationDialogService {
    
      constructor(private modalService: NgbModal) { }
    
      public confirm(
        title: string,
        message: string,
        btnOkText: string = 'OK',
        btnCancelText: string = 'Cancel',
        dialogSize: 'sm'|'lg' = 'sm'): Promise<boolean> {
        const modalRef = this.modalService.open(ConfirmationDialogComponent, { size: dialogSize });
        modalRef.componentInstance.title = title;
        modalRef.componentInstance.message = message;
        modalRef.componentInstance.btnOkText = btnOkText;
        modalRef.componentInstance.btnCancelText = btnCancelText;
    
        return modalRef.result;
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 2019-06-21
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多