简体   繁体   English

Angular 2 Bootstrap(ngx-bootstrap / ng2-bootstrap)下拉菜单不起作用

[英]Angular 2 Bootstrap (ngx-bootstrap/ng2-bootstrap) dropdown not working

I can only seem to get Angular Bootstrap dropdowns to work in one part of my application. 我似乎只能使Angular Bootstrap下拉列表在我的应用程序的一部分中工作。 I'm not sure why it works there and not in other places. 我不确定为什么它在那里而不是在其他地方起作用。 In my module, I'm importing with BsDropdownModule.forRoot() just like it is where it works. 在我的模块中,我将使用BsDropdownModule.forRoot()导入,就像它可以正常工作一样。 I've tried pasting in HTML from several of their examples... Nothing. 我已经尝试从他们的几个示例中粘贴HTML了。 I know I had it working there before, and don't remember changing anything that had to do with it. 我知道我以前在那里工作过,并且不记得更改任何与它有关的内容。

I'm not getting any errors and I'm not exactly sure how to proceed with troubleshooting this issue. 我没有收到任何错误,也不确定如何继续解决此问题。 I noticed the problem with ng2-bootstrap, so I upgraded to ngx-bootstrap. 我注意到ng2-bootstrap的问题,所以我升级到ngx-bootstrap。 Same problem. 同样的问题。

Make sure that your versions of Angular and that of Bootstrap are compatible. 确保您的Angular版本和Bootstrap版本兼容。 It happened when I was using Angular 4 with ng2-bootstrap 1.6.x. 当我在ng2-bootstrap 1.6.x中使用Angular 4时发生了这种情况。 Better yet, instead of ng2-bootstrap you should be using ngx-bootstrap. 更好的是,您应该使用ngx-bootstrap而不是ng2-bootstrap。 To get the dropdown working, add the container attribute: 要使下拉菜单正常工作,请添加container属性:

<div class="btn-group" dropdown  container="body"></div>

I figured out the issue. 我发现了问题。 The dropdowns were actually working. 下拉菜单实际上是有效的。 The issue was that for some reason the y-position (top) for the .dropdownMenu was placing it beyond the bounds of the container it resided in, where it could not be seen. 问题是由于某种原因,.dropdownMenu的y位置(顶部)将其放置在其所在容器的边界之外,无法看到该容器。 I'm still not sure why THAT is happening, but at least I have something to work with. 我仍然不确定为什么会发生这种情况,但至少我有一些需要配合的地方。

i was able to get this working with bootstrap@4.0.0-alpha.5, but when i switched to 4.0.0-alpha.6 i could not get the dropdown to work. 我能够使用bootstrap@4.0.0-alpha.5使它工作,但是当我切换到4.0.0-alpha.6时,我无法使用下拉菜单。 Not sure what went wrong but I had to add BsDropdownModule into context by adding to my providers (I already had the BsDropdownModule.forRoot()).... (not exactly sure of details of what i had broken in the end). 不知道出了什么问题,但是我不得不通过将BsDropdownModule添加到我的提供程序中来添加到上下文中(我已经有了BsDropdownModule.forRoot())....(不确定到底我破坏了什么的细节)。

BUT found that adding BsDropdownModule to the module where i was calling the menu then it worked. 但是发现将BsDropdownModule添加到我在其中调用菜单的模块中,然后它起作用了。

您需要在AppModule导入中添加BsDropdownModule.forRoot(),并在使用引导程序下拉菜单的子模块中添加BsDropdownModule(不是.forRoot()!)。

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

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