简体   繁体   English

如何从角度的ts文件打开模型?

[英]How to open model from ts file of angular?

I try to open the model on my page is load我尝试在我的页面上打开模型正在加载

HTML HTML

<ng-template #content let-c="close" let-d="dismiss">
      <div class="modal-header">
        <h4 class="modal-title" id="modal-basic-title">Hi there!</h4>
        <button type="button" class="btn-close" aria-label="Close" (click)="d('Cross click')"></button>
      </div>
      <div class="modal-body">
        <p>Hello, World!</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-dark" (click)="c('Save click')">Save</button>
      </div>
    </ng-template>
    
    <button class="btn btn-lg btn-outline-primary" [hidden]="true" (click)="open(content)" data-target="#varify_warning">Launch demo modal</button>

TS file TS文件

ngOnInit(): void 
  {
    document.getElementById("varify_warning")?.click();
  }

But the model didn't open?但是模型没有打开?

I want to trigger the click() event on the Launch demo modal button from ngInit()我想从 ngInit() 触发启动演示模式按钮上的 click() 事件

Open ngbModal on button click单击按钮打开 ngbModal

On your .ts file, add this:在您的.ts文件中,添加以下内容:

  open(content: any) {
    this.modalService.open(content);
  }

And in your .html file, add this:在您的.html文件中,添加以下内容:

    <button (click)="open(content)">Open modal</button>
    <ng-template #content let-c="close" let-d="dismiss">
       modal content goes here
    </ng-template>

Open ngbModal on ngOnInit在 ngOnInit 上打开 ngbModal

On your .ts file, add this:在您的.ts文件中,添加以下内容:

        @ViewChild("content",{static:true}) content:ElementRef;
        ngOnInit(): void {
            this.modalService.open(this.content);
        }

And in your .html file, add this:在您的.html文件中,添加以下内容:

    <ng-template #content let-c="close" let-d="dismiss">
       modal content goes here
    </ng-template>

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

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