Updating variable in a higher-level component in Angular 7

Currently, I am utilizing Angular 7. Within my child component displayed in the Stackblitz example below, I have encountered an obstacle. Although I can access my variable on the parent control by using @Input, I am unable to change it. Could you provide guidance on how to achieve this within the given Stackblitz example? The primary goal is to hide the modal by setting displayPopup=false upon clicking the hide button.

STACKBLITZ

Answer №1

To solve the issue, you should add an EventEmitter to the popup.component.ts

Below is a functioning example:

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

@Component({
  selector: 'app-popup',
  templateUrl: './popup.component.html',
  styleUrls: ['./popup.component.css']
})
export class PopupComponent {

  @Output() public close: EventEmitter<void> = new EventEmitter();

  closeModal() {
      this.close.emit();
  }
}

Then in the parent component, you can listen for that event like so:

<app-popup *ngIf="showPopup" (close)="showPopup = false;"></app-popup>

Answer №2

If you are looking to implement a custom two-way binding solution, check out this StackBlitz example:

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

@Component({
  selector: 'app-popup',
  templateUrl: './popup.component.html',
  styleUrls: ['./popup.component.css']
})
export class PopupComponent implements OnInit {

  private _bool: boolean;
  @Output() boolChange = new EventEmitter();

  set bool(value) { this._bool = value; this.boolChange.emit(value); }
  @Input() get bool() { return this.bool; }

  constructor() { }

  ngOnInit() {
  }

  hideModal() {
    this.bool = false;
  }
}

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: Unable to establish connection - Asp.Net Core and Angular 10

Currently, I am following an Asp.net tutorial along with using the Angular server with CORS package integration. My main goal is to make a post request: // POST: api/Trackers // To protect from overposting attacks, enable the specific properties y ...

Angular 2: A Glimpse into the File Contents

Currently working on a project where I need to aggregate multiple XML files with the same structure. The process involves users dropping their XML files, making some edits, and receiving an XML file with all the necessary data organized in a list of elemen ...

Invalid component prop provided to ButtonBase in Material UI. Ensure that the children prop is correctly rendered in this custom component

Forgive me for asking a basic question, as I am not the most proficient frontend developer and have searched extensively online. Whenever I inspect my frontend application in Chrome, I keep encountering this error. (3) Material-UI: The component prop pro ...

What drawbacks come with developing an Express.js application using TypeScript?

Curious about the potential drawbacks of using TypeScript to write Express.js applications or APIs instead of JavaScript. ...

Guide to executing specific functions based on selected dropdown options in Angular 5

Several drop down menus need to be implemented. The requirement is that when an option from the drop-down is selected, a corresponding custom function should be called. However, in the code below, what should replace opt.mtd since it is not functioning as ...

Styling with react-jss based on intricate conditionals

After experimenting with react-jss for a few weeks, I am faced with the challenge of styling components efficiently. With numerous conditionals in these components, I am striving to avoid creating an excess of new components that essentially share the same ...

npm start is unable to locate any instances of Angular version 2

I attempted to follow the instructions provided in the Angular 2 quickstart guide. I must say, I encountered a dreadful error. Can anyone translate the following to English? I did execute npm install. However, using "npm run lite" does work. npm start & ...

Resetting a field in a child component using Angular parent component communication

I'm a bit confused about how to reset a field in a child component from the parent. In my setup, there's a field in the child component that should be reset when a function is called in the parent component. Child component: @Component({ ...

How can Angular JS handle multiple validators being triggered at once?

Hey there, I'm currently working with validators in my Angular form setup. Here's a snippet of how it looks - I utilize Validators.compose to combine custom validators. The errors from these validators are then displayed on the HTML component. My ...

Having trouble deciding between flatMap and concatMap in rxJs?

Having trouble grasping the distinction between flatMap and concatMap in rxJs. The most enlightening explanation I found was on this Stack Overflow post about the difference between concatMap and flatMap So, I decided to experiment with it myself. import ...

TypeScript error TS2531: Object may be null

<div > <input type="file" id="uploadImage"/> <button @click="Upload" style="width: 80px;height: 30px;" > upload ...

Tips for creating a custom waitForElementText function in Playwright

I need to implement a function called waitForElementText() in playwright. For example, I have headers labeled with the CSS selector '.header-name' on each page. When navigating from the Home page to the Users page, I provide two parameters to ...

Nexus and GraphQL: The root typing path for the "context" type is not found

I’m currently working on integrating GraphQL into Next.js API routes. For writing the GraphQL schema, I’m utilizing Nexus. Here are the two essential files: context.ts and schema.ts, that help in setting up Nexus development mode. // context.ts import ...

How do I implement an array of objects using an interface in React and Typescript?

I'm working with an array of objects where the data is stored in a JSON file. Here's a glimpse of the JSON file: [ { "_id": "62bd5fba34a8f1c90303055c", "index": 0, "email": "<a href="/cdn-cgi/l/emai ...

Expanding the Warnings of React.Component

When I create a new class by extending React.Component in the following manner: export default class App extends React.Component<any, any > { constructor (props: React.ReactPropTypes) { super(props); } // other code } I encountere ...

Linking Two HTML Components in Angular 4 with Identical Values

Recently, I've started working with Angular and encountered an issue. In a table row, the value item.md_id is bound like this: <tr *ngFor="let item of driverData"> <td class="align-right" id="md_id" [(ngModel)]="item.md_id" name="driverId ...

Is it possible to include parameters in an HTML GET request with Electron.Net?

I have successfully implemented a function in an Angular component within an Electron application using HttpClient: var auth = "Bearer" + "abdedede"; let header = new HttpHeaders({ "Content-Type": 'application/json&a ...

Can wildcard paths be imported in React using Typescript?

Is there a way to dynamically import a React Typescript Component from a wildcard path, similar to the following code snippet? const Component = loadable( () => import(`../../../src/**/*/${component_name}`), ); I have searched numerous solutions on ...

The name '__DEV__' is not discoverable at the moment

While working with the mobx library in my project, I encountered an issue after installing it using npm. Upon exploring the mobx/src/error.ts file within the node_modules folder, I came across a compile time error on line 78: const errors: typeof niceError ...

Access the child component within an @ChildComponent directive in Angular

Is it possible to retrieve child components of another component? For instance, consider the following QueryList: @ContentChildren(SysColumn) syscolumns: QueryList<SysColumn>; This QueryList will contain all instances of the SysColumns class, which ...