Display an element in Angular2 when a selection is changed

I am working with Angular 2 and typescript. I have a requirement where I only want to display the Save button if a new option is selected, and then hide the Save button after it is clicked. I'm not sure how to approach this problem. Below is the code snippet:

HTML

<select>
   <option *ngFor="let option of optionArray" [value]="option">{{option}}</option>
</select>

<span class="input-group-btn">
   <button class="btn btn-primary save-button" type="submit">Save</button>
</span>

Answer №1

To make a dynamic change to the submission button based on the value selected in a select field, you can use ngModel.

<select [(ngModel)]="selectedValue">
   <option *ngFor="let option of optionArray" [value]="option">{{option}}</option>
</select>

<span class="input-group-btn" *ngIf="selectedValue">
   <button class="btn btn-primary save-button" type="submit">Save</button>
</span>

Please Note: Don't forget to import and include FormsModule inside your AppModule's NgModule.

Answer №2

If you find yourself in this situation, consider using *ngIf.

<select [(ngModel)]="selected">
   <option *ngFor="let option of optionArray" [value]="option">{{option}}</option>
</select>
<span *ngIf="selected" class="input-group-btn">
       <button class="btn btn-primary save-button" type="submit">Save</button>
</span>

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

The HTML5 datalist feature is only capable of displaying the first suggestion from the filtered array for autocomplete

I can't seem to display all the elements suggested in the datalist except for the first one. I've double-checked and confirmed that my array contains more than one element. It's puzzling why the other elements are not showing up in the sugge ...

Tips on sending values to register in react-hook-form 7

As of version 7, react-hook-forms no longer use refs. I have been attempting to create a dynamic form by taking field props from a JSON file, but every time I try to register the useForm, I encounter a type issue. const {register , and so on..}= useForm({d ...

The EC2Service's CapacityProviderStrategies is encountering an issue with the message: "The specified capacity provider named ****** could not be

After reading this resolved article, I decided to set up two AutoScalingGroup, two AsgCapacityProvider, and two ecs on ec2 service Now, my goal is to attach these CapacityProvider to each service. However, I encountered an error message stating: The spec ...

Understanding the status of HTTP requests or observing the updates of observables in Angular2/Typescript is essential

I've been working on an Angular2 and Typescript application where I'm utilizing Angular2's HTTP methods to retrieve data from a database within a service. The service is triggered inside a component's onInit() function and I'm able ...

Using TypeScript with async await operators, promises, and the memoization pattern

I am currently in the process of updating my code to incorporate the latest TypeScript enhancements. We have implemented various memoization patterns, with the main goal being to ensure that services with multiple subscribers wait for one call and do not t ...

Tips for displaying the date of a JSON response in Angular HTML format?

When working with Angular HTML, I am looping through a JSON array using ngFor and accessing the birth date data like this: <td>{{item.customer_info.birth_date}}</td> The data is being displayed as ddMMyyyy format, but I would like to change it ...

The exportAs property for matAutocomplete has not been specified

Issue Detected An error occurred with the directive "exportAs" set to "matAutocomplete" ("-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto"> I implemented code referenced from https://material.angular.io/components/auto ...

Application Initialization Error: appInits is not a valid function

When my Angular v17 application starts, I need to set some important values right away. This is how it's done in app.config.ts: export const appConfig: ApplicationConfig = { providers: [ ConfigService, ... { pr ...

Encountering errors during 'npm i' - issues arising from unresolved dependency tree

Recently, I have been facing issues with running npm i as it keeps failing and showing an error. The project is using Angular 15 without any previous errors, so it's puzzling why there is suddenly a complaint about Angular 16. npm ERR! code ERESOLVE n ...

Parameters for constructing classes in TypeScript

I've been exploring different coding styles in TypeScript recently. When it comes to initializing an object from a class, what are the advantages and disadvantages of these two code styles in TypeScript? class Class3 { // members private rea ...

Angular 7: Finding the variance between array elements

How can I subtract the values from the first 3 rows of the table? The formula is TVA Collectée - TVA Déductible - TVA Déductible/immo If the result is positive, it should be displayed in the box labeled TVA à Payer. If it's negative, it should g ...

Issue with retrieving value from local storage promise in Ionic 2+ and Angular 4.3

After successfully logging in and obtaining the token value by passing in the username and password from a function, I encountered a null value on the next page because the values were not passed. This issue was resolved when I encapsulated the token retur ...

Tips for seamlessly incorporating an uploaded image into my personal Imgur Album

After setting up an application on ImgUr and obtaining both the ClientID and ClientSecret, I have encountered an issue with adding images to my album. https://i.sstatic.net/6gZp6.png Despite knowing my unique album id (e.g., xbvhXo), attempts to upload i ...

What is the best way to enforce input requirements in Typescript?

I am currently facing an issue with two required inputs that need to be filled in order to enable the "Add" button functionality. I have considered using *ngIf to control the visibility of the button based on input values, but it seems to not be working. ...

Breaking down an object using symbols as keys in Typescript

I'm encountering an error when running this code Type 'symbol' cannot be used to index type '{ [x: string]: string; }'.: let symbol = Symbol() let obj = { [symbol] : 'value'} let { [symbol]: alias } = obj // ...

"Looking to swap out the Angular theme's CSS stylesheet for your entire application? Here's

I was initially facing an issue while trying to import CSS through index.html as I kept getting a MIME type error: enter image description here The browser refused to apply the stylesheet from 'http://localhost:4200/css/pink-bluegrey.css' because ...

Issue with Angular trackBy not functioning properly within a nested *ngFor loop

My component is similar to the following <div class="users-list" *ngIf="datasetPermission !== undefined"> <div *ngFor="let userpermission of datasetPermission; trackBy : trackByFn"> <span *ngFor="let user of userpermission.users"& ...

The recursive component is functional exclusively outside of its own scope

I'm facing an issue where my recursive component is not nesting itself properly. The problem arises when I try to use the Recursive component inside another Recursive component. Although the root is correctly inserted into the Recursive component fro ...

Tips for securely accessing a parameterized property of an object in Typescript

I need to create a function that takes an object as an argument and accesses a specific property of this object based on another parameter. Here is the code snippet: // javascript code function setProperty(subject, property, value) { subject[property] ...

Configuration files and dynamic link libraries included in the Electron software package

I'm trying to figure out how to package dlls and configuration files using electron-builder or electron-packager. Despite searching the documentation for both tools, I haven't found an example of where to place these files when packaging the appl ...