Angular6: Parent component fails to pass data to child component

My goal is to implement a loading message that displays when the page is loaded. I have created a loader component as a child component, and I am passing a value from the parent. However, for unknown reasons, the loader is not being displayed.

I have attempted the following code:

I set up a component for the loader with the following content:

loader.ts

@Input() showLoader: boolean = false;

loader.html

<div class="modal-backdrop in" [style.display]="showLoader ? 'block' : 'none'">

When trying to display this loader from the main component, I pass the value like so:

html

<app-loader [showLoader]="loaderOn"></app-loader>

ts

public loaderOn: boolean = false;

ngOnInit() {
    this.loaderOn = true;
}

Answer №1

Your code seems like it should work, but there might be a missing piece or an error hiding in the shadows. To highlight the change better, I introduced a 2-second delay in the ngOnInit();

Key parts of app.component.ts:

  loaderOn: boolean = false;

  ngOnInit() {
    setTimeout(() => {
      this.loaderOn = true;
    }, 2000)
  }

Essential snippets from app.component.html:

Value sent from the parent: <mark>{{loaderOn}}</mark>
<app-loader [showLoader]="loaderOn"></app-loader>

Important bits from loader.component.html:

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

@Component({
  selector: 'app-loader>',
  template: `
    <p>Value received in the child: <mark>{{showLoader}}</mark> </p>

    <div class="modal-backdrop in" [style.display]="showLoader ? 'block' : 'none'">
    ...this is the div which we wanted to style...
    </div>
  `,
  styles: [`*{color:blue}`],
})
export class LoaderComponent {
  @Input() showLoader: boolean = false;

}

For a complete solution, check out this live example on StackBlitz

Answer №2

Is there a reason you're not considering using ngIf instead?

loader.html

<div class="modal-backdrop in">

You can easily hide and show like this:

<app-loader *ngIf="loaderOn"></app-loader>

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

Error message in MS Edge API: 'browser has not been defined'

I'm currently in the process of developing an extension for the new Microsoft Edge browser. Upon loading the unpacked extension, I encountered the following error: Uncaught ReferenceError: browser is not defined According to the Microsoft Edge docu ...

Encrypting href links in Nodemailer with Handlebars for forwarding purposes

I am working on a project that involves utilizing NodeMailer + Handlebars for sending and tracking emails. I am interested in changing every href link to the project URL before redirecting to the destination link. For example: Original email: <a href ...

Angular applications encountering issues with Express delete-route functionality

For some reason, I am having trouble with Delete routes in my Angular applications. They seem to work perfectly when tested using Postman, but fail to function when called from within the Angular app. //Attempting to call a delete route from an Angular app ...

Bring in items and then go through each one

I'm curious if there's a way to loop through imported objects? import { Row, Col, Form, FormItem, Icon, Input, Tooltip, Image, Button, Dialog } from 'element-ui' objects.forEach(object => { // do something here }) When I have a ...

The Vue feature responsible for displaying information on the webpage is currently not working as expected

I'm in the process of developing a settings page for a project. This particular page is based on HTML and utilizes JSON to store data, with Vue 3 being used to display the information on the page. However, I've encountered an issue where the data ...

The persistent issue with Vue.js is the constant occurrence of undefined problems

Hello, I am new to working with Vue and I am currently trying to utilize props in my component. However, I am facing an issue where the prop data is always undefined and showing as $attrs in the Vue-Debugger. My Plan: I intend to use the TabWrapper Compon ...

The type does not contain a property named `sort`

"The error message 'Property sort does not exist on type (and then shoes4men | shoes4women | shoes4kids)' pops up when attempting to use category.sort(). I find it puzzling since I can successfully work with count and add a thousand separato ...

Connecting data from a .ts file in one webpage to the HTML content of another webpage

I am currently working on implementing the binding concept in Angular. The code snippet below is from PageOne.ts: this.navCtrl.push("PageTwo",{ paramType: params, }) Here is a snippet from PageTwo.html: <span>{{paramType}}</span> ...

Inject Angular 2 component into designated space

I am working on a website that requires a settings dialog to be loaded in a designated area upon clicking a button. The settings dialog is a component that retrieves data from REST endpoints. I am hesitant to simply insert the component and hide it as I ...

Issue with THREE.js Line Object not displaying in web browser

Having just recently started to dabble in THREE.js, I encountered an issue where the code only displays a black screen instead of the intended triangle shape. Can someone assist me in troubleshooting the problem within the code? <!DOCTYPE html> &l ...

Updating Rails record using Ajax with select_tag and onchange functionality

I'm working on an index view where I want to update a table data dynamically using select_tag onchange feature without the need to refresh the page. Do I need to use Coffeescript to detect the onchange event, capture the new value, and then send it to ...

I require assistance in displaying a dynamic, multi-level nested object in HTML using AngularJS

I have a complex dynamic object and I need to extract all keys in a nested ul li format using AngularJS. The object looks like this: [{"key":"campaign_1","values":[{"key":"Furniture","values":[{"key":"Gene Hale","values":[{}],"rowLevel":2},{"key":"Ruben A ...

Error: Unable to locate module: Issue: Unable to find '../components/charts/be.js' in '/vercel/workpath0/my-app/pages'

Having some trouble deploying my next.js app through Vercel. Everything works fine locally with the command 'npm run dev'. But when attempting to deploy it on Vercel using a Github remote repository, I encountered the following error: 18:07:58.29 ...

What is the process for converting a UTC datetime string into the local timezone of users?

I am utilizing Laravel 5.7 for the API and Vue for the frontend development. The API response includes a user's last seen timestamp in UTC format by default, like this: last_seen: "2019-04-17 05:20:37". Laravel API code: $user_details = array( ...

Google Bar Graphs Cannot Display Decimal Values

Having an issue with my bar chart from Google Chart not displaying float numbers. Other types of bar charts provided by Google Chart work fine, but this specific type doesn't show the float numbers. Here is the code I have written: http://jsfiddle.ne ...

Exported data in Vue3 components cannot be accessed inside the component itself

Essentially, I'm working on creating a dynamic array in Vue3. Each time a button is clicked, the length of the array should increase. Below is the code snippet. <div class="package-item" v-for="n in arraySize"></div> e ...

The JQuery File-Upload plugin remains inactive even after a file has been chosen

I am currently working on integrating the JQuery File-Upload plugin (). The issue I'm facing is that it doesn't respond when a file is selected. Here are some potential problems to consider: No errors appear in the Chrome console. Selecting a ...

Using JavaScript to generate JSON data in a plain text format rather than using HTML

I have a snippet of code that retrieves the user's location every 5 seconds. <div id="geo" onLoad=""></div> <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPo ...

The active class in the Vue.js component navigation only takes effect after the second click

Within a menu component placed inside a view accessed through its own route (/how-it-works), I am facing an issue with the header component structure, outlined below: <nav class="nav"> <ul class="top-menu"> <li v-for="menu in me ...

Using React's state to hide a div reveals the div with a flicker when saving the information

Is there a way to create a hidden div that the user can show with a button in React Next.js without it flashing on reload? const [showBanner, setShowBanner] = useState(true); useEffect(() => { const data = window.localStorage.getItem('STATE& ...