Collapse navbar does not work in angular 6

Asked

Viewed 724 times

1

People, I’m trying to make a navbar component using Bootstrap 4 and when I put on mobile the button to show the rest of the menu it doesn’t work

when I do with an HTML, CSS and JS it works smoothly, but when I try to "migrate" to the angular it doesn’t work.

angular json.

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

The HTML of Navbar

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
  <ul class="navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Features</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Pricing</a>
    </li>
    <li class="nav-item">
      <a class="nav-link disabled" href="#" tabindex="-1" aria- 
                disabled="true">Disabled</a>
    </li>
  </ul>
 </div>
 </nav>

navbar.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css'] 
})
export class NavbarComponent implements OnInit {

constructor() { }

 ngOnInit() {
  }

}

I even added the Bootstrap scripts but still no fuinciona

  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" 
    integrity="sha384- 
    q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
    crossorigin="anonymous"></script>
  <script 
    src="https://cdnjs.cloudflare.com/ajax/libs
      /popper.js/1.14.6/umd/popper.min.js" integrity="sha384- 
      wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" 
      crossorigin="anonymous"></script>
  <script 
      src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1
         /js/bootstrap.min.js" integrity="sha384- 
         B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" 
       crossorigin="anonymous"></script>
  • 1

    Where did you add these Cdn files? Remembering that it is highly "contraindicated" to use CSS frameworks that use jQuery with Javascript frameworks like Vue, Angular, etc... for N reasons, as conflicts to give only one example.

  • @Leandrade added Cdn both in navbar.component.html and even in index.html, however I did not have a result, I tried to use rgx-bootstrap, but I did not find anything that could use a navbar

  • 1

    Strange guy, pq put himself in the index was to work, but anyway, take a look at this post on medium which has an example of implementing hamburger without jquery: https://medium.com/@tiboprea/build-a-Responsive-bootstrap-4-navbar-in-angular-5-without-jquery-c59ad35b007

  • @Leandrade I will check!

1 answer

0


Your problem is that in data-target of button you wrote the wrong value without the # identifier of the target. The value to do the target properly have to have a # in front of the name.

See that you put it like this:

data-target="navbarNav"

It should be like this, with the # in front of the value:

data-target="#navbarNav"

Note that I didn’t change anything in your code, I just put the # in the value of data-target of button and the navbar started to function as expected!

<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Features</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Pricing</a>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
            </li>
        </ul>
    </div>
</nav>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.