简体   繁体   English

带有 Bootstrap 5 的 Angular 13:如何以编程方式打开模式

[英]Angular 13 with Bootstrap 5: How to open a modal programmatically

I am using Angular 13 and have included Bootstrap 5 in my project by linking to the CDN links like shown here:我正在使用 Angular 13,并通过链接到 CDN 链接将 Bootstrap 5 包含在我的项目中,如下所示:
https://getbootstrap.com/docs/5.0/getting-started/introduction/#css https://getbootstrap.com/docs/5.0/getting-started/introduction/#css
and here:和这里:
https://getbootstrap.com/docs/5.0/getting-started/introduction/#bundle https://getbootstrap.com/docs/5.0/getting-started/introduction/#bundle

Everything works fine as far as components are concerned, but I cannot find a way to open a modal programmatically.就组件而言,一切正常,但我找不到以编程方式打开模式的方法。 I don't won't to hide the modal using display: none/block , because this method doesn't include the animations that come with the component (like dropping from the top of the screen etc).我不会使用display: none/block隐藏模式,因为此方法不包括组件附带的动画(例如从屏幕顶部掉落等)。

The component works fine if I use the button that triggers the behaviour, just like here https://getbootstrap.com/docs/5.0/components/modal/#live-demo如果我使用触发行为的按钮,该组件可以正常工作,就像这里https://getbootstrap.com/docs/5.0/components/modal/#live-demo

For now, my solution is to keep this button with display: none and trigger a manual click in my code, but this seems hacky to me.现在,我的解决方案是让这个按钮display: none并在我的代码中触发手动点击,但这对我来说似乎很棘手。

Is there a way to achieve this without using ng-bootstrap?有没有办法在不使用 ng-bootstrap 的情况下实现这一点?


EDIT:编辑:

Eliseo's comment seems to be in the right direction, by running npm install @types/bootstrap, vscode recognizes Modal from 'bootstrap'. Eliseo 的评论似乎是在正确的方向,通过运行 npm install @types/bootstrap,vscode 从“bootstrap”识别出 Modal。 However, running ng serve, gives me this error但是,运行 ng serve 给了我这个错误

Error: Module not found: Error: Can't resolve 'bootstrap' in ...

Using by ElementRef you can do this check this out.使用ElementRef你可以这样做检查一下。

<button #open type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
  Launch static backdrop modal
</button>

<!-- Modal -->
<div class="modal fade"   id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button #close  type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Understood</button>
      </div>
    </div>
  </div>
</div>

. .

export class AppComponent implements OnInit{
  @ViewChild('open',{read: ElementRef})closeRef!:ElementRef;
  @ViewChild('close',{read: ElementRef})modelRef!:ElementRef;
 
 
 ngOnInit(): void {
     
   setTimeout(() => {
    console.log('hua');
    
     this.closeRef.nativeElement.click()
   }, 100);
   setTimeout(() => {
    console.log('hua');
    
   
    this.modelRef.nativeElement.click()
   }, 10000);
 }
}

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

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