Steps to reveal a concealed div when a button is pressed and hide the button in Angular 2

Having trouble with Angular UI development, particularly with the following requirement:

  • When the Add Button is clicked, a hidden form should be displayed.
  • Clicking the Add Button should also hide it.
  • The hidden form includes a Cancel button.
  • If Cancel is clicked, the form should hide and the Add Button should reappear.

I attempted to achieve this using Angular 2 template syntax and nested boolean values, but haven't found the ideal solution yet.

How can I accomplish this in Angular 2 or 4? Do I need to utilize a host listener or event emitter for this task? Below is a snippet of my code along with a link to a Plunker demo:

template.html

<button(click)="addParameter=addParameter==true?false:true">
              Add</button>

            <div class="Parameters" *ngIf="addParameter==true">

            <input name="hello">

            <button (click)="hideForm();">Cancel</button>
            </div>

test.ts

export class App {

  private addParameter:boolean=false;

}

https://plnkr.co/edit/fa3Pdea1mB4RztAgOAW2?p=preview

Answer №1

There are a few ways to achieve this. Depending on whether you want to handle it in the component or the template. Personally, I like to keep the template clean and do the "work" in the component. In that case, your code would look something like this:

<button *ngIf="!addParameter" (click)="toggleForm()">Add</button>
<div class="Parameters" *ngIf="addParameter">
  <input name="hello">
  <button (click)="toggleForm()">Cancel</button>
</div>

and in the TypeScript file:

toggleForm() {
  this.addParameter = !this.addParameter
}

DEMO: http://plnkr.co/edit/y3bDxsXMTYLtf8HI2PlA?p=preview


If you prefer to do this in the template, it would look like this:

<button *ngIf="!addParameter" (click)="addParameter = !addParameter">Add</button>
<div class="Parameters" *ngIf="addParameter">
  <input name="hello">
  <button (click)="addParameter = !addParameter">Cancel</button>
</div>

DEMO: https://plnkr.co/edit/xcSzXGWOMNIhuZ83OXbs?p=preview

Answer №2

Perhaps the solution you're seeking is along the lines of this:

<div>
      <form>
        <input name="hello">
        <button *ngIf="addingForm===false" (click)="addingForm = true">Add</button>
        <button *ngIf="addingForm===true" (click)="addingForm = false">Cancel</button>
      </form>
      <form *ngIf="addingForm===true">
        <input name="hidden">
      </form>
    </div>

In TypeScript:

addingForm = false;

You can also refer to this example: https://plnkr.co/edit/uXztfHwdWxuTVNIg6sxA?p=preview

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

Please add content prior to the <input type="image"> tag

To improve the code for adding a product to the basket, I want to enhance the buy button by incorporating an image or background fill with HTML text on top of it. <input border="0" src="/images/buy.png" type="image" /> ...

Tips for generating a single CSS file that adapts to various screen sizes without the ability to modify the class names within the library

I am currently learning ReactJs and JavaScript and I have a question on how to optimize this css setup effectively. I am utilizing the react-responsive library to detect screen sizes and determine the best layout for my project. The issue I am facing is ...

The Cascading of Bootstrap Card Designs

Looking for some assistance with my TV Show Searcher project that is based on an API. The functionality is complete, but I'm struggling to get the Bootstrap cards to stack neatly without any empty space between them. I want it to resemble the image ga ...

What is the best way to integrate jQuery Masonry with ES6 modules?

Attempting to utilize the npm package https://www.npmjs.com/package/masonry-layout Following the installation instructions, I executed: npm install masonry-layout --save Then, in my file, import '../../../node_modules/masonry-layout/dist/masonry.p ...

"Simply click and drag the preselected item into the viewer to add it

I am looking to create an angular page with specific functionality: Enable users to upload a document (no problem) Display the document's content Allow users to drag a predetermined object onto the document display area to indicate where they will si ...

Utilizing Promise.all() for handling multiple promises with multiple arguments

Currently, I am dynamically constructing a list of entries utilizing data retrieved from the server. Each entry is associated with a parent. My objective is to extract the parent's name from the DOM by locating its ID. This process works effectively ...

Generating SVG images that show up as black in light mode and switch to light colors in dark mode

I am currently working on a website that has the option to switch between light and dark mode, and I need my logo to adjust accordingly. The black elements of my logo should appear black in dark mode and light in light mode. I attempted using the CSS cod ...

Confirming changes to checkbox values in Angular 2 prior to updating

My current challenge involves implementing a confirmation dialog in my application, and I'm feeling a bit unsure about the logic behind it. UserDetailsComponent.ts import { Component, OnInit, OnDestroy, ViewChild, Input, OnChanges, SimpleChange } f ...

Error in Node.js Express: Attempted to access property 'image' of null resulting in an unhandled error event

I've been trying to identify where the issue lies. Can someone lend a hand with this? When attempting to submit the post without an image, instead of receiving a flash message, the following error pops up: and here's the source code link: https: ...

Adjust the size of a DIV once all AJAX requests have been completed within a .getScript function in JavaScript

I'm facing an issue with dynamically resizing a DIV after all content is loaded using jQuery. This is the structure of my HTML page: html <div id="limit" style="position: relative; overflow: hidden;"> <div class="contentpanel> ...

ChakraUI ForwardRef in React not applying variant to child component

Encountering an issue with the combination of forwardRef and Input from ChakraUI. Attempting to create a generic input component with a flushed variant, but facing difficulty as Chakra keeps resetting it back to default. ModalInput Component import { for ...

How to find the length of an array in Node.js without utilizing JQuery

Is it possible to determine the length of the Dimensions array in nodejs? The array can have 1 or 2 blocks, and I need to write an if condition based on this length. Since this code is inside an AWS-Lambda function, using JQ may not be an option. For exam ...

Enhance the appearance of your Vuejs application with conditional style formatting

I want to enhance a basic table by applying conditional CSS styles to the cells based on their values. For example: If area1 == 'one', then assign the td-one CSS style. <tr v-for="version in versions" :key="version.id"> < ...

Positioning the Submenu in Elementor Site

I've been working on creating a vertical navigation menu with a horizontal submenu for my website using Elementor. While I've made some progress, I'm struggling to position the submenu correctly. My goal is to have the submenu display next t ...

Looking for specific styles within CSS classes

I am looking to identify all the classes with styling attributes defined using either vanilla JS or jQuery. Specifically, I want to find classes that have the border style defined along with its value. It would be great if I could also modify these classes ...

Unable to retrieve DateTime.Now as a string within a Razor view

I am trying to retrieve the current time in a Razor View and utilize it in JavaScript, as demonstrated below: @{ string fileName = "Score_List_" + DateTime.Now.ToShortDateString(); } <script> // assigning C# variable to JavaScript variabl ...

Optimizing Bootstrap 4 Carousel for various screen sizes

I want to implement a full screen bootstrap 4 carousel with a transparent navbar at the top. The provided code seems to be functioning correctly. HTML: <!doctype html> <html lang="ro"> <!-- START head --> <head> &l ...

Sharing AppSettings between an Angular project and ASP.NET Core in a seamless manner

Currently, I have a project set up using the VS 2022 ASP.NET Core with Angular template. The project itself is working well, but I am facing a challenge in trying to integrate the Angular app with the .NET Core's appsettings.json file for configurati ...

What is the process of displaying JSON headers using JavaScript?

Although it may seem like a simple question, I am new to JSON. Can someone explain how to display a heading in the console? This is the code I have: var jsonstr = '{"profile":{"name" : "raj","age":"35&qu ...

The background appears after the drop down menu text

I created a menu with a drop-down effect using CSS3 transitions. However, my issue is that the text is visible before the background appears, which goes against the intended design. Here is a link to my JSFiddle. HTML: <nav> <ul> ...