The child component is failing to display the parent value that was passed via @Input

// PARENT app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass']
})
export class AppComponent {
  title = 'personal-finance-client';
  profile : Profile = new Profile();

}

// PARENT app.component.html

<app-navbar *ngIf="profile" [profile]="profile" [title]="title" />
<div class="font-bold text-3xl">
  {{title}}
  {{profile.getName()}}
</div>

// CHILD navbar.component.html

<p>{{myTitle}}</p>
// CHILD navbar.component.ts

import { Component, Input, OnInit } from '@angular/core';
import { Profile } from '../entity/Profile';

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

  @Input()
  set title(value: string | undefined){
    this._title = value;
    if(value){
      this.myTitle = value;
    }

  }

  get title() {
    return this._title;
  }

  @Input() profile!: Profile;

  profileName: string = this.profile?.getName();
  myTitle!: string;

  ngOnInit(): void {
      alert(this.profile?.getName())
  }
}

I had trouble rendering the `myTitle` in the child `navbar.component.html` file with the parent's title expected to be 'personal-finance-client'. Upon debugging by placing console.log inside `OnInit` of `navbar.component.ts`, it initially displayed as undefined, but then switched to the actual value of 'personal-finance-client'.

It appears that the child component captures only the initial `OnInit` value and does not update when a new value is detected?

Is this standard behavior because I am unable to successfully transmit the parent's value to the child's value in this scenario?

I attempted to implement `OnChanges` and direct the changed value to the new field, however, no value is reflected on the screen.

Answer №1

I believe I have pinpointed the issue - I originally set up the app with Sass, then switched back to CSS manually, causing a rendering error likely related to Sass.

Upon creating a fresh Angular project, I confirmed that the parent value is correctly rendered from the child component.

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

What is the process for server-side rendering an Angular application with Angular Universal and incorporating an SSL certificate?

Typically, when I want my angular applications to run locally over HTTPS, I install a certificate and make changes in the angular.json file like this: "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { " ...

Tips for enlarging a chart in Highcharts within a Bootstrap modal when clicking on a thumbnail chart in Angular 6

My goal is to have Highcharts render to different div IDs. When I click on a thumbnail chart, the same chart should expand in a Bootstrap modal. I used the Hello Angular 6 StackBlitz editor for a demo, but since it's not saving properly, I'll sha ...

Error in Angular: Unexpected '<' token

After setting up my angular and express app using the angular-cli and express command lines, I successfully built the angular app with the ng build command. However, when attempting to serve it with the express server, I encountered the following error in ...

Tips on retrieving a strongly typed value from a method using Map<string, object>

Having had experience working with C# for a while, I recently ventured into a Node.js project using TypeScript V3.1.6. It was exciting to discover that TypeScript now supports generics, something I thought I would miss from my C# days. In my C# code, I ha ...

How can I display an agm-polyline within a map in Angular 7?

I need assistance with adjusting the polylines on my map and dynamically setting the zoom level based on their size and position. Here is the code I am currently using: <agm-map style="height: 500px" [latitude]='selectedLatitude' [longitude ...

Is it possible to use Angular 5+ to retrieve a file from an FTP server

I recently attempted to utilize the npm library known as ftp-client (https://www.npmjs.com/package/ftp-client) to download a basic image from my ftp server within my Angular 5 project. Despite copying and pasting the provided example code, I am encounterin ...

The release of the Angular App within a webjar is causing problems relating to the baseHref

Currently, I am looking to package my Angular frontend in a webjar so that it can be easily imported via Maven into any Java backend. Successful in this task, I now have a Spring Boot backend with an application.properties file showing: server.servlet.cont ...

Unable to fetch packages from npm or github using jspm install because of proxy configuration restrictions

I'm attempting to execute a basic Angular 2 unit test application. I have cloned the following git repository and followed the steps provided in the readme file: https://github.com/matthewharwood/Hit-the-gym I have configured proxy settings for npm, ...

The onChange event does not work as expected for Select controls

I am facing an issue with my common useForm.tsx file when handling the onChange event for select controls. The error message I encounter is displayed below. Does anyone have any suggestions on how to resolve this? Error: Type '(e: ChangeEvent<HTM ...

What is the best way to troubleshoot the type of data being sent to the subscriber of an Angular observable?

I am new to the world of Angular-7, RxJS-6, and Visual Studio Code. Currently, I am facing a challenge with debugging an Observable that is being returned to a subscriber, resulting in a "TypeError" at runtime. It seems like this issue is not uncommon amon ...

Elements are receiving CSS properties from other elements within the same component

I'm encountering an issue with my components: 1. I cannot seem to style html and body in signin.component.css for some reason. The workaround I found was using: encapsulation: ViewEncapsulation.None ==> This works perfectly However, when I switch ...

Achieve HTTPS with angular-cli when running ng serve

It seems like the code snippet below is not working as expected. ng serve --ssl true --ssl-key <key-path> --ssl-cert <cert-path> Even when creating the Certificate and key by placing them in the default ssl directory, there is still no effect ...

Troubles in app.module arise post-transition from Angular 15 to Angular 16

While attempting to upgrade my Angular application from version 15 to 16, I ran into a multitude of errors within my app.module. Despite trying various troubleshooting steps, such as checking module imports and utilizing ng update, the errors persist. It a ...

What is the solution for resolving the error message "Property '' does not exist on type 'typeof import'"?

Hi everyone, I'm a new Angular developer who is working on implementing authentication. I recently added the auth0-angular package to my project and now I'm encountering an error when creating a WebAuth() instance in my code editor (VS Code). Th ...

Send users to the login page if they are not authenticated

Why is my middleware crashing after redirecting to the login page? import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; // To avoid crashing, this function can be marked as `async` if using `await ...

Using *ngFor to Retrieve JSON Data Multiple Times

I'm trying to fetch 3 images from my endpoint that contains a large number of images using Angular's *ngFor directive (or any other suitable method) Below is my component.html <div class="col-sm-4 col-xs-6" *ngFor="let result o ...

Issue: Module not found error encountered while applying decorators

I am attempting to incorporate decorators into my TypeScript class. Here is what I have done: import { Model, ModelCtor } from 'sequelize/types'; function decorator(constructor) { // } @decorator class Service implements IService { // ...

Tips for modifying the language of an Angular Application's OneTrust Cookie Banner

I'm currently developing an Angular application and utilizing OneTrust for managing cookie consent. The issue I'm encountering is that while the rest of the components on the login page are properly translated into the target language, the OneTru ...

Prevent Click Event on Angular Mat-Button

One of the challenges I'm facing involves a column with buttons within a mat-table. These buttons need to be enabled or disabled based on a value, which is working as intended. However, a new issue arises when a user clicks on a disabled button, resul ...

An error is being encountered with web-animations-js on IE 9, showing the following message: "SCRIPT5007: Object expected; web-animations.min.js (15

Currently, I am utilizing Angular 5. Below are the steps I followed: Firstly, added web-animations-js as a dependency : npm install web-animations-js Then, uncommented the following line in polyfils.ts import 'web-animations-js'; // Run npm ...