Angular - Enabling the next screen button only after completing multiple selections

Currently, I'm working on a screen where users can select multiple options from a table. The requirement is that they must select at least 3 options before they can proceed. However, I am facing difficulties in implementing this functionality and unsure of what to research.

At the moment, I have an array of values stored in my component:

values = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8", "Option 9", "Option 10", "Option 11", "Option 12"];

(Please note that these values will be fetched from an API later on, but for now placeholder data suffices)

These values are displayed on the screen and users can select or deselect them without any issues. Now, I need to restrict the selection to only 3 choices and enable the next button once 3 selections have been made.

Here is the current code for the selection buttons:

<div *ngFor="let value of values">
    <button class="selection_btn text-center" #btn (click)="btn.classList.toggle('selected')">{{value}}</button>
  </div>

Next Button Functionality:

next() {
    this.router.navigate([`${appRoutesNames.PAGE_2}`]).then();
}

Please bear in mind that the actual route name in my file differs from the placeholder used here.

Answer №1

Form controls serve a specific purpose.

If needed, you can customize them with the following code:


let control = new FormControl([], [
  Validators.minLength(3), 
  Validators.maxLength(3), 
  Validators.required
])


<button 
  #btn 
  class= "custom_btn text-center"
  (click)="control.setValue(control.value.concat([value]))"
>{{value}}</button>

To validate, use this check:

<button [disabled]="!control.valid" [routerLink]="['page2']">Proceed to next</button>

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 occurs when attempting to filter data through input text pasting in Angular

Currently, I am working on a web application that utilizes the Angular framework and angularfire2. The issue I am encountering is related to the data filter functionality not functioning correctly when pasting copied text. Interestingly, it works perfectly ...

Approach to Global Components in Angular 2

What is the optimal method for ensuring a component is accessible globally, such as a login modal? Create a new instance in each component view Dynamically instantiate a new one when needed Declare it in the AppComponent, create a function to handle it ...

What is the best way to incorporate a module from an external 'include' folder in your code?

Within my project's tsconfig.json, I have specified the following: "include": [ "src/**/*", "generated**/*" ] In the directory, there exist two files named src/main.ts and generated/data.json. The task at hand is to be able to successfully ...

Tips for implementing a reusable instance of a class in (SSR) React (comparable to a @Bean in Spring)

I am currently developing a new application using Next.js and React (Server Components) in TypeScript. In order to showcase and evaluate the app, I need to support two different data sources that provide identical data through separate APIs. My goal is to ...

Inject a dynamic component into an Angular 2 Modal Service

Struggling to dynamically insert a component into a custom modal window component. The modal component is given a URL to an HTML file containing a component: <a modal bodyUrl="/some/path/body.html"> body.html: <hello-component></hello-co ...

What is the best way to incorporate multiple Bootstrap templates into an Angular project without causing conflicts between them?

When incorporating a bootstrap template into the admin interface, it is important to consider that its design may vary from the template used for visitors. Mixing multiple styles based on different templates can lead to conflicting styles and can potenti ...

Troubleshooting: Issues with Angular2 compatibility on Safari version 9.1.2

I am encountering an issue with running my angular2 app on Safari 9.1.2. It works fine on all higher versions of Safari as well as other browsers such as Chrome, Firefox, Opera, and Edge. However, when I try to run it on Safari 9.1.2, I receive the followi ...

Angular 7 - Merging Objects and Arrays

I have two models, Userc (with fields name, phone, email, etc.) and Usercco (same as Userc with an additional field coname for company name). In Userc model, I have coid field representing the company id. In my database (Firebase), I only have Userc and C ...

Simulating @Input data for an Angular component using Jest

As we transition our Jasmine tests to Jest in our Angular project, we are encountering issues when attempting to mock @Input values of components in the tests. Previously, in Jasmine, we would write code like this: @Component({ selector: 'app-messag ...

The React component was not able to receive any children as input

My Typescript-written React component is called GradientText.tsx: import React, { ReactNode } from "react" import { cn } from "@/lib/utils" const typeMap = { h1: "h1", h2: "h2", p: "p", } inte ...

Exploring Objects within an Array in Ionic 2 and AngularJS 2

I am currently working on displaying reviews obtained from a Laravel API, showcasing feedback on various meals. The goal is to create a slideshow of review messages along with additional data as presented in the array of Objects below: { "2": { ...

Understanding how to interpret error messages received from a server within an Angular component

Below is the code I am using for my backend: if (err) { res.status(400).send({ error: "invalid credentials", data: null, message: "Invalid Credentials" }); } else { res.status ...

Guide on adding a button to a mat-table in Angular

I am looking to add a delete button or an Angular trash icon to a mat-table in an Angular application. Can anyone guide me on how to achieve this? Here is my current table code: <mat-table #table [dataSource]="ELEMENT_DATA"> <ng-container cdk ...

Utilizing PatchValue with nested arrays for dynamic form handling in Angular

Trying to create a form builder similar to Google Forms using the code from this link. I have successfully created a quiz form and added it to the database. Now, I'm looking to build an "edit quiz form" page. Here is a snippet of the full code: im ...

Please explain the significance of the question mark in the statement `impliedTokenType: ?string`

Stripe elements have a question mark before the type in this GitHub repository: link The syntax I expected was impliedTokenType?: string, but it actually is impliedTokenType: ?string. Can someone explain the difference between the two? ...

Encountering an issue with the property name despite already defining it

Encountering a property name error even though it has been defined Uncaught (in promise): TypeError: Cannot read property 'nome' of undefined export class HomePage { inscricao = "São Bernardo"; nome = "abc"; nomeInvalido; construc ...

Using ngFor to connect input with the Algolia Places feature

I have implemented an Algolia Places input within an ngFor loop using Angular8. The issue I am facing is that the (change) event only works properly after typing in the input for the second time. While this is functional, it's not exactly the behavior ...

Is it feasible to securely remove an item from an array within an object without the need for any assertions in a single function?

My interest in this matter stems from curiosity. The title may be a bit complex, so let's simplify it with an example: type ObjType = { items: Array<{ id: number }>; sth: number }; const obj: ObjType = { sth: 3, items: [{ id: 1 }, { id: 2 } ...

Angular is programmed to detect any alterations

Upon detecting changes, the NgOnChanges function triggers an infinite service call to update the table, a situation that currently perplexes me. Any assistance on this matter would be greatly appreciated. Many thanks. The TableMultiSortComponent functions ...

How to Hide Parent Items in Angular 2 Using *ngFor

I am dealing with a data structure where each parent has multiple child items. I am trying to hide duplicate parent items, but accidentally ended up hiding all duplicated records instead. I followed a tutorial, but now I need help fixing this issue. I only ...