Visibility of an Angular 2 directive

It's frustrating that I can't change the visibility of a reusable directive in Angular2.

1) Let's say I have a login page.

I want to control the visibility of my navbar based on whether I am on the login page or logged in. It should be hidden on the login page and visible after logging in.

@Component({
    selector: 'my-app',
    template: `
               <navbar [visible]="isLoggedIn"></navbar>
               <div class="container"> 
                    <router-outlet></router-outlet>
               </div>`,
    directives: [NavbarComponent,ROUTER_DIRECTIVES]
})

In my app.component.ts file, I have set the initial value of isLoggedIn to false, which hides the navbar. Toggling between true and false works, indicating that it functions correctly from app.component.

  isLoggedIn=false;

However, setting the visibility in my login page does not work as expected.

How do I communicate with the navbar component to update its visibility when I log in? I have added it as a provider, but even after setting this.isLoggedIn = true, the navbar remains hidden.

import { Component } from '@angular/core';
import ... (remaining content omitted for brevity)

Below is the code snippet for my navbar.compoment.ts:

import {Component, Input} from '@angular/core';
import ... (remaining content omitted for brevity)

And here is my navbar.html file:

<div *ngIf="visible">
  <div class="dashhead">
    <div class="dashhead-titles">
      <h6 class="dashhead-subtitle">Bootops</h6>
      <h3 class="dashhead-title">Role Builder</h3>
    </div>

    <div class="dashhead-toolbar">
      <span class="dashhead-toolbar-divider hidden-xs"></span>
    </div>
  </div>

  <ul class="nav nav-bordered">
    <li [class.active]="isCurrentRoute(['Environments'])"><a 
    <li><a (click)="logout()">Logout</a></li>

  </ul>
</div>

Answer №1

Give this a shot:

<navbar [hidden]="!isLoggedIn"></navbar>

Sorry, but the visible html attribute doesn't exist.

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

How to display an [object HTMLElement] using Angular

Imagine you have a dynamically created variable in HTML and you want to print it out with the new HTML syntax. However, you are unsure of how to do so. If you tried printing the variable directly in the HTML, it would simply display as text. This is the ...

Derive the property type based on the type of another property in TypeScript

interface customFeatureType<Properties=any, State=any> { defaultState: State; properties: Properties; analyzeState: (properties: Properties, state: State) => any; } const customFeatureComponent: customFeatureType = { defaultState: { lastN ...

Assigning a click event to an element within CKEditor

Looking to add a click event to an element in ckeditor4-angular for custom functionality <div class="fractional-block" id="fractional-block"><span>5</span><svg height="5" width="100%"><line ...

IntelliSense is failing me. I am unable to find information on DOM/CSS types

In the midst of starting my inaugural TypeScript project, I encountered a rather selective compiler: let test = <div style={{textAlign:'right'}}>Text</div>; // OK let right = 'right'; let test2 = <div style={{textAlign: ...

Oops! There seems to be an issue with locating a differ that supports the object '[object Object]' of type 'object', like an Array

I'm currently encountering an error that reads: (ERROR Error: NG02200: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays. Did you mean to use the key ...

Instantiate a fresh object with its own set of functions

I've been struggling with implementing the OnPush change detection strategy. Here is an example of a class I have: class Item { private name; private _valid; public set valid(valid) { this._valid = valid; } constructor(name) { th ...

How can an Angular 2 component detect a change in the URL?

My Angular 2 component subscribes to a service based on the URL hash without calling any subcomponents. I am wondering if I should use Route or Location for this scenario, and how can I listen for and react to a pop state event? ...

Enforce directory organization and file naming conventions within a git repository by leveraging eslint

How can I enforce a specific naming structure for folders and subfolders? I not only want to control the styling of the names (kebab, camel), but also the actual names of the folders and files themselves. For example, consider the following paths: ./src/ ...

Importing an SDK in Angular is causing a frustrating issue where a blank white screen is

Currently, I'm facing an issue with integrating @eth-optimism/sdk into my Angular project. Despite importing the sdk using import * as optimismSDK from "@eth-optimism/sdk" and attempting to utilize its functions like CrossChainMessenger, Angular displ ...

Testing a subclass in Angular 6 using Karma and the @Input decorator

After finding no answers to related questions, I've decided to pose my own specific case regarding unit testing an Angular 6 component that is a subclass of a base component. The base component itself includes an @Input decorator and looks like this: ...

Is there a way to modify the login component to utilize the username instead of the email for logging in?

I recently developed a Spring Boot API which requires the username and password for login authentication. I am now trying to modify the ngx admin interface to accept the username instead of the email as the credential. Can anyone provide guidance on how ...

What is the process for generating an attribute in a system?

If I want to create an element using plain JavaScript, here is how I can do it: var btn = document.createElement('button'); btn.setAttribute('onClick', 'console.log(\'click\')'); document.body.appendChild( ...

What could be the reason for Angular 2 not recognizing this valid regex pattern?

The regular expression shown below is considered valid (check here): ^(\+27|27|0)\s?(\d{2})[-\s]?(\d{3})[-\s]?(\d{4})$ Despite its validity, Angular 2 throws the following error: EXCEPTION: Error in ./App class App - i ...

What is the best way to implement lazy loading for child components in React's Next.js?

I am exploring the concept of lazy loading for children components in React Next JS. Below is a snippet from my layout.tsx file in Next JS: import {lazy, Suspense} from "react"; import "./globals.css"; import type { Metadata } from &quo ...

Nestjs struggles with resolving dependencies

Having trouble finding the issue in my code. (I'm still new to nestjs and trying to learn by working on some apps). The console log is showing the following error: Nest can't resolve dependencies of the UrlsAfipService (?). Please ensure tha ...

"Sequencing http.get requests in Angular 2 using

In my service, I have a series of http.get requests structured as follows: constructor(private http:Http) {} getDetails(sysID:string){ var details; this.http.get('https://blahURL').map(res => res.json().filter(f => f.id == another.id)[0] ...

React Routing: Unleashing the Power of Multi-Level Routing

In my quest to create a route with multiple levels (<Route path="/hello/world" element={<a>hello world</a>} />), I encountered a few issues. Here are the versions I am using: react: 18.1 react-router-dom: 6.3.0 Success with O ...

Is there a way to simulate the BeforeInstallPromptEvent for testing in Jasmine/Angular?

I'm currently working on testing a dialog component that manages the PWA install event triggered manually when a specific function is invoked. The goal is to handle the promise returned by the BeforeInstallPromptEvent.userChoice property. However, wh ...

Differentiating between model types and parameters in Prisma can greatly enhance your understanding of

Consider the following scenario: const modifyData = async(data, settings) => { await data.update(settings) } In this case, the data refers to any data source, and the settings consist of objects like where and options for updating the data. How can ...

Testing React Hook Form always returns false for the "isValid" property

When creating a registration modal screen, I encountered an issue with the isValid value when submitting the form. In my local environment (launched by npm start), the isValid value functions correctly without any issues. However, during unit testing us ...