英文:
Angular CDK Menu opens at the bottom of the page
问题
I've implemented the CDK Menu from the documentation (doc) in my app.
Here's what I've done:
-
Install the CDK:
npm i @angular/cdk
-
Import the menu cdk in
app.module.ts
and use it in the "imports" section:import { CdkMenuModule } from '@angular/cdk/menu' imports: [BrowserModule, CdkMenuModule]
-
Create the menu in my
app.component.html
:<button [cdkMenuTriggerFor]="menu" class="example-standalone-trigger">Click me!</button> <ng-template #menu> <div class="example-menu" cdkMenu> <button class="example-menu-item" cdkMenuItem>Refresh</button> <button class="example-menu-item" cdkMenuItem>Settings</button> <button class="example-menu-item" cdkMenuItem>Help</button> <button class="example-menu-item" cdkMenuItem>Sign out</button> </div> </ng-template> <hello-world></hello-world>
The problem is that when I click on the button, the menu opens at the bottom of the page instead of just under the button.
Here's a Stackblitz that shows the problem: Stackblitz Link
I've followed this example from the documentation (their Stackblitz works correctly): menu-with-standalone-trigger
英文:
I've implemented the CDK Menu from the documentation (doc) in my app.
Here's what I've done:
-
Install the CDK:
npm i @angular/cdk
-
Import the menu cdk in
app.module.ts
and use it in the "imports" section:
import { CdkMenuModule } from '@angular/cdk/menu'
imports: [BrowserModule, CdkMenuModule]
-
Create the menu in my
app.component.html
<button [cdkMenuTriggerFor]="menu" class="example-standalone-trigger">Click me!</button> <ng-template #menu> <div class="example-menu" cdkMenu> <button class="example-menu-item" cdkMenuItem>Refresh</button> <button class="example-menu-item" cdkMenuItem>Settings</button> <button class="example-menu-item" cdkMenuItem>Help</button> <button class="example-menu-item" cdkMenuItem>Sign out</button> </div> </ng-template> <hello-world></hello-world>
The problem is that when I click on the button, the menu opens at the bottom of the page instead of just under the button.
Here's a Stackblitz that shows the problem: https://stackblitz.com/edit/f1y9wh
I've followed this example from the documentation (their stackblitz works correctly): menu-with-standalone-trigger
答案1
得分: 5
Sure, here's the translated content:
如果有人遇到这个问题,您需要在您的 styles.scss
中导入以下内容:
@import '@angular/cdk/overlay-prebuilt.css';
这是来自 CDK Overlay 的:https://material.angular.io/cdk/overlay/overview
但是不知何故,CDK 菜单文档中没有写明您需要导入这个CSS...
英文:
Ok so if anyone have this problem, you have to import this in your styles.scss
:
@import '@angular/cdk/overlay-prebuilt.css';
It's coming from the CDK Overlay: https://material.angular.io/cdk/overlay/overview
But somehow it's not written in the CDK Menu documentation that you have to import this CSS...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论