Attempting to conceal certain elements based on the current route in Angular 5

Trying to create a dynamic navigation system where different items are displayed based on the website path. For example, if the path is http://mywebsite.com/path, certain navigation items should be hidden while others are shown.

My current setup includes...

app.component.html

<ul (click)="navLinkClick()" class="nav-page_ul">
    <a *ngIf="!routeHidden" [routerLink]="['']"><li class="go-to-website">go to site</li></a>
    <a *ngIf="routeHidden" [routerLink]="['1']"><li>1</li></a>
    <a *ngIf="routeHidden" [routerLink]="['2']"><li>2</li></a>
    <a *ngIf="routeHidden" [routerLink]="['3']"><li>3</li></a>
    <a *ngIf="routeHidden" [routerLink]="['4']"><li>4</li></a>

app.component.ts

ngOnInit() {
this.router.events.subscribe( (e) => {
  if (e instanceof NavigationStart) {
    if (e.url === '/6' || e.url === '/7' || e.url === '/8' || '/9') {
      this.routeHidden = false;
    } else {
      this.routeHidden = true;
    }
  }
});
}

The issue I'm facing is that regardless of the route, only the "go-to-website" navigation item is being displayed and none of the rest. I'm unsure of what mistake I might be making...

Thank you for your help!

Answer №1

If you are facing this scenario, the expression e.url === '/8' || '/9' could potentially pose a problem

To resolve it, update the code to e.url === '/8' || e.url === '/9'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Suggestions for automatically adjusting the URL based on the browser's geographical location in Asia?

Is there a way to achieve the following scenario? I am looking to update the URL if the user's browser is from Asia. For example, if I visit a URL with the domain my.XXX.com, the application will detect the browser's location and automatically ch ...

Creating HTML tags using Kotlin

Looking to generate HTML using Kotlin in the browser, I experimented with the Kotlinx library. However, I found that it lacks support for callbacks like the following: div { onclick = { event -> window.alert("Kotlin!") } } Are there an ...

What is the most effective method for sorting through vast amounts of data with Javascript, React, and Redux?

Currently, I am working with a minimum JSON data set of 90k [...] and utilizing the .filter method. Everything is functioning correctly without any issues, however, from a performance standpoint, I am curious about potential improvements. Any suggestions o ...

Is it possible to execute an HTTP request using a functional resolver?

Previously, I used a class-based resolver where I could inject HttpClient in the constructor. However, this approach has now been deprecated - see the source for more information. Now, I am looking to implement an HTTP get request in a functional resolver ...

Utilizing D3.js to filter nodes and links by making multiple selections from a checkbox

I am interested in enhancing my current forced directed graph by adding more flexibility. You can access the Jsfiddle here. Currently, there are radio buttons that control the opacity of nodes and links. There are 4 types of nodes: Agent, Customer, Pho ...

JavaScript 2 add or remove a class to the keyboard when opening or closing

As I work on developing an app in Ionic 2, I am facing a challenge where the background image needs to change when the keyboard is opened and closed in an input field. <ion-content padding class="bg_gogreen"> <ion-list> <ion-item class=" ...

The redirection to the webpage is not occurring as expected

I've been attempting to redirect another webpage from the homepage in my node server. However, the redirect isn't working as intended to link to idea.html, where the relevant HTML file is located. Can anyone assist me in identifying any errors in ...

What is the process for applying a class in jQuery to empty HTML elements?

Working on a WordPress/PHP website and looking to dynamically add a class when child elements are empty? This is the HTML structure: <div class="featured-block"> <a href="/" class="featured-block__item cf"> <div class="featured-bl ...

Controlling page events with asynchronous webmethod results in JavaScript to trigger or prevent actions

Utilizing a webmethod to determine if the user has permission to "Delete a record". Below is the initial JavaScript code prior to implementing access control. $(".apply-delete-msg").live('click', function() { return confirm("Are you sure you ...

Incorporating React into a non-React website

I am currently working on a project where the server renders all views using the Twig template engine. Therefore, I tend to write all my scripts within the .twig templates. Take, for instance, the aside-buttons.twig template: <div class="aside-butto ...

Which specific html container or element can be found on the mymsn pages?

When accessing mymsn, users have the ability to personalize the content and layout of their webpage. I am curious about what type of container is being utilized for this customization - does it involve an html element, or perhaps javascript, or something e ...

Guide to switching content within a DIV when the up and down buttons are pressed using JavaScript and jQuery

I have a functional code with a timeline where each event is connected to the other. There are buttons to delete and copy data, but I want to implement functionality for swapping elements when the up or down button is clicked. I have provided a sample code ...

Filtering Key Presses in Quasar: A Comprehensive Guide

Seeking Assistance I am looking to integrate an "Arabic keyboard input filtering" using the onkeyup and onkeypress events similar to the example provided in this link. <input type="text" name="searchBox" value="" placeholder="ب ...

Unable to persist cookies while utilizing npm's request library

I'm currently working on setting up a local environment where I'm attempting to log into a service. In the client side, I'm using the 'request' library, and on the service side, I'm utilizing Express and express-session. Whil ...

Utilizing Variables in TypeScript to Enhance String Literal Types

Here are my codes: export const LOAD_USERS = 'LOAD_USERS'; export const CREATE_USER = 'CREATE_USER'; export interface ACTION { type: string, payload: any } I am trying to limit the possible values for ACTION.type to either 'L ...

Defining the scope of a variable that is accessed by multiple asynchronous callbacks

Utilizing the async.js library by Caolan McMahon and the jQueryUI progress bar, I am providing real-time feedback to users while multiple asynchronous calls gather data and populate elements in a complex graph. The lingering question is: What is the most ...

Change the field of view (FOV) of the Three.js camera according to

Is there a way to modify the standard Three.js resize code so that the scene scales with the DOM element's width as it does with the height? Currently, the scene scales in relation to the element's height, but when the width decreases, the model ...

Safari having trouble auto-playing Vimeo iframe embed

Update 6/26/23: Seems like a mysterious change occurred, as now the Vimeo video on project pages is playing automatically for me in Safari without any specific reason. It's working fine on Chrome too. Not sure if Vimeo made an update or if it's r ...

Retrieve every item in a JSON file based on a specific key and combine them into a fresh array

I have a JSON file containing contact information which I am retrieving using a service and the following function. My goal is to create a new array called 'contactList' that combines first names and last names from the contacts, adding an &apos ...

Setting dynamic fields and their corresponding values within a form: a comprehensive guide

Currently, I am utilizing Angular form Control. In my form, I have a combination of static and dynamic values to display. For the static values, I am using formControlName in the HTML file and defining in the TypeScript file as shown below. Here is how I ...