Dropdown menu in latest Angular

Viewed 184

I have the following directive for a custom dropdown menu:

import { Directive, ElementRef, HostBinding, HostListener } from "@angular/core";

@Directive({
    selector: '[appDropdown]'
})
export class DropdownDirective {

    @HostBinding('class.open') isOpen = false;

    @HostListener('document:click', ['$event']) toggleOpen(event: Event) {
        this.isOpen = this.elementRef.nativeElement.contains(event.target) ? !this.isOpen : false;
    }

    constructor(private elementRef: ElementRef) {
        //
    }
}

I have included the references in my app.module.ts of course.

The following is what I have put in my header.component.html:

<div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li><a href="#" (click)="onSelect('recipe')">
                        Recipes
                    </a>
                </li>
                <li><a href="#" (click)="onSelect('shopping-list')">
                        Shopping List
                    </a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li class="dropdown" appDropdown>
                    <a href="#" class="dropdown-toggle" role="button">
                        Manage
                        <span class="caret">
                        </span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="#">
                                Save Data
                            </a>
                        </li>
                        <li><a href="#">
                                Fetch Data List
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>

I changed my class from navbar-default to collapse.

The following is the Manage button that is supposed to have the dropdown which includes my appDropdown directive:

<div class="btn-group" appDropdown>

            <button type="button"
                    class="btn btn-primary dropdown toggle">
                    Manage Recipe
                    <span class="caret"></span>
            </button>

            <ul class="dropdown-menu">
                <li><a href="#">Add Ingredients to Shopping List</a></li>
                <li><a href="#">Edit Recipe</a></li>
                <li><a href="#">Delete Recipe</a></li>
            </ul>

        </div>

I cannot find the issue in my code. Once I changed the class from defaut to collapse, that menu does not even appear...

What am I doing wrong?

EDIT: My style.css is empty. The content of my package.json and angular.json are as follows:

{
  "name": "app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.2.9",
    "@angular/common": "~11.2.9",
    "@angular/compiler": "~11.2.9",
    "@angular/core": "~11.2.9",
    "@angular/forms": "~11.2.9",
    "@angular/platform-browser": "~11.2.9",
    "@angular/platform-browser-dynamic": "~11.2.9",
    "@angular/router": "~11.2.9",
    "bootstrap": "^5.0.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1102.8",
    "@angular/cli": "~11.2.8",
    "@angular/compiler-cli": "~11.2.9",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.1.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.1.5"
  }
}

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "APP": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/APP",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "APP:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "APP:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "APP:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "APP:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "APP:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "APP"
}
0 Answers
Related