Angular CLI - exploring the depths of parent-child component communication

My issue revolves around accessing the 'edit' method of a child component using @ViewChild, but for some reason it's not functioning as expected. Where could I possibly be going wrong?

Here are the console logs: https://i.sstatic.net/wvpVN.jpg

Key parts of the CompanyComponent class:

Vital notes: 1) Importing CompanyRegistrationComponent 2) Having @ViewChild on that component 3) Ensuring that the openEditModal method is correctly called in the HTML file

    import { Component, OnInit, ViewChild } from '@angular/core';
    ...
    export class CompanyComponent implements OnInit {
      ...
      private companyRegistration: CompanyRegistrationComponent;
      ...    
      ngOnInit() {
       this.getCompany();
       this.subscribeCurrentUser();
       this.buildForm();
      }
       private openEditModal(editedCompany){
         this.companyRegistration.edit(editedCompany);
      }

Crucial segments of the CompanyRegistrationComponent class:

    import { Component, OnInit, ViewChild, ElementRef, NgZone } from '@angular/core';
    ...
    export class CompanyRegistrationComponent implements OnInit {

      ...
      private companyRegistration: SemanticModalComponent;
      ...
      ngOnInit() {
        this.setFormValidations();
        LocalizationHelper.configureLocationAutocomplete(this.mapsAPILoader, this.locElementRef, this.ngZone, this.form)
      }

      open(){
       this.companyRegistration.show();
      }
      edit(editedCompany){
        this.companyRegistration.show();
      }

I've attempted to @ViewChild that class in various other places, yet the issue persists.

Template:

 <sm-button *ngIf="editButton && !showTransactionButton" class="positive" (click)="openEditModal(company)">Edit</sm-button>

Answer №1

The issue arises from the following line of code:

this.companyRegistration.edit(editedCompany);

This error occurs because companyRegistration is undefined due to how it is declared:

@ViewChild(CompanyRegistrationComponent) companyRegistration: CompanyRegistrationComponent;

You need to declare the ViewChild with the same name as in the parent Component's template: ./company.component.html :

<app-company-registration #acr></app-company-registration>

Therefore, in your ./company.component, you should write:

@ViewChild('acr') companyRegistration: CompanyRegistrationComponent;

I hope this explanation resolves your issue :)

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

Utilizing conditional date parameter in Angular's in-memory web API for an HTTP request

In my current project, there is a collection of objects referred to as members members = [ {id:'1', firstName:'John', lastName:'Black', birthDate:'1956-11-22', gender:'Male', email:'<a href="/c ...

Iterate over Observable data, add to an array, and showcase all outcomes from the array in typescript

Is there a way to iterate through the data I've subscribed to as an Observable, store it in an array, and then display the entire dataset from the array rather than just page by page? Currently, my code only shows data from each individual "page" but ...

What causes the variation in Http Post Response between the Console and Network response tab?

Currently, I am tackling an issue in angular2 related to HTTP post response. The problem arises when my endpoint is expected to return a boolean value. Interestingly, the response appears correctly in Chrome's Network response tab; however, upon loggi ...

Guide to starting a Tizen Web App project using Angular

Starting out: I have experience with Angular and am now looking to delve into Tizen for the first time. I want to create a Tizen Web Application using Angular (7.x.x) for Samsung TV. After installing Tizen Studio and its extensions, I've set up a st ...

Issue with font-size changes using css variables in Angular not updating in browser for specific fields

Utilizing CSS variables, I have implemented a feature that allows users to adjust the font size to small, medium, or large. While this functionality works correctly for most fields, there are certain instances where the value is applied but not displayed. ...

Experiencing a problem in React JS when trying to render a component?

I encountered an error message while trying to render a component from an object. Type 'FC<Boxprops> | ExoticComponent<{ children?: ReactNode; }>' is not compatible with type 'FC<Boxprops>'. Type 'ExoticComponen ...

Retrieving the output from a nested scope within a function

I have a scenario where I am working with a function that has a nested "then" function containing a return statement. My goal is to combine this inner return data with the outer return data. Below is the code snippet: public async getAllWidgets(): Promis ...

I am facing an issue with the PrimeNG time picker as it is not letting me modify the selected time

Currently utilizing PrimeNG for its calendar functionalities, I have been experiencing difficulty in getting the time picker to function properly. Despite my attempts, the time selector does not allow me to make any changes. Below is the code snippet from ...

Navigational bar mishaps: troubleshooting the "is not a function error" in layout.tsx

I recently started learning React (Next.js) and I'm working on adding a navbar feature to my project. The goal is to have the active navlink stand out when the user is on the corresponding page. To achieve this, I created a function that updates the s ...

The behavior of the dynamically generated object array differs from that of a fixed test object array

I'm facing an issue while trying to convert JSON data into an Excel sheet using the 'xlsx' library. Everything works perfectly when I use test data: //outputs excel file correctly with data var excelData = [{ test: 'test', test2: ...

Limit choosing to group child elements within ag-grid

Is there a way to disable row selection in ag-grid specifically for the rows used to group the grid? For example, clicking on rows labeled with "United States" and "2008" should not trigger selection. Only rows like the one highlighted in blue should be se ...

Change object values to capital letters

Upon retrieving myObject with JSON.stringify, I am now looking to convert all the values (excluding keys) to uppercase. In TypeScript, what would be the best way to accomplish this? myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", ...

The marker is not updating in real-time with the actual latitude and longitude on the Google Maps angular

I have been attempting to update the marker in my Angular Google Map in real-time, but unfortunately it is not working as expected. It only displays the initial static data and then fails to update. Despite conducting a thorough search on Google, I have be ...

Angular 6 application experiencing an issue where the browser console is not displaying an error that says "Cannot GET /

I was tasked with making modifications to an Angular 6 web application, which I hadn't worked on before. After receiving the source code, I attempted to display the website but encountered errors. I spent late nights and early mornings trying to succe ...

Need for utilizing a decorator when implementing an interface

I am interested in implementing a rule that mandates certain members of a typescript interface to have decorators in their implementation. Below is an example of the interface I have: export interface InjectComponentDef<TComponent> { // TODO: How ...

Tips for resolving the issue: "Encountered error loading module script..." in Angular 8 and Electron 5

I have been working on developing an Electron 5 app using Angular 8. Despite following numerous online tutorials, I keep encountering the same error. Initially, I set up a new project and ran ng serve --open, which successfully displayed the default angul ...

What is the best approach to eliminate the 'false' type within a setState function in React?

Hey, I've been working on a project that involves using the useState hook and dealing with state using generics. I encountered an issue where I manipulated a fetched array within a setState function using the filter method, which resulted in returnin ...

The Typescript compiler will continue to generate JavaScript code even if there are compilation errors

As a fresh learner of TypeScript, I have been experimenting with some basic concepts. Below is the code from my file app1.ts: class Monster { constructor(name, initialPosition) { this.name = name; this.initialPosition = initialPosition ...

Guidelines for integrating a SOAP asmx service into an Angular+8 application using XML implementation

Here is the code snippet I'm struggling with: var xmlItemAll = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x ...

Discover the method for converting a local file into a string

Looking to retrieve and read an SVG file in Angular 6 from the assets folder as a string. I've attempted the following: this._htmlClient.get('../../assets/images/chart1.svg').subscribe(data => { console.log('svg-file: ', ...