What is the best way to extract information from a button and populate a form in AngularCLI?

I am currently attempting to enhance my Angular App by using a button to transfer information to a form upon clicking, rather than the traditional radio buttons or select dropdowns. My objective is to incorporate HTML content into the button (such as Mat-Icon). Unfortunately, I am facing some challenges as I have not been successful in embedding the data into the form without relying on radio buttons, select options, or regular input fields.

Initially, I experimented with the following:

<div class="flex-wrapper" [formGroup]="subForm">
   <mat-form-field formControlName="resourceName" >   
        <input type="button" *ngFor="let res of resources" [value]="res">
    </mat-form-field>
</div>

Subsequently, I tried:

<div class="flex-wrapper" [formGroup]="subForm">
    <mat-form-field formControlName="resourceName">
      <button type="button" *ngFor="let res of resources"[value]="res">
        {{res}}
      </button>
    </mat-form-field>
  </div>

Despite testing these two options, neither proved to be effective and I would prefer for the second method to work. The form exists as a subform within a child component of a parent component/form group. Additionally, it is integrated with Angular Material Stepper at the parent level.

Answer №1

Managed to develop a setter function for manipulating the desired data element.

setResource(value:string){
this.subForm.setValue({resourceName: value});}

I kickstarted the process with:

(click)="setResource(res)" *ngFor="let res of resources" type="input"

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

Strategies to prevent fortuitous success in testing

I have the following test case: it('is to display a welcome message', () => { spyOnProperty(authServiceSpy, 'token').and.returnValue(environment.testAuthenticationToken); let teacher: Teacher = authServiceSpy.token.teacher; ...

Is there a way to ensure that a certain block of code in Typescript is executed only after an API call has been completed?

When making an API call, I need the code after the call to wait until the API call finishes. In my function called this.api_call, it calls the API and only returns an array returnValue once the call is complete. let returnValue = this.api_call(data); // ...

Unable to locate the term "module"

I've been working on a TypeScript file that includes an exported function called sum: This script is meant to be run in Node.js. function sum(a:number):number{ return a; } module.exports.sum=sum; I'm encountering some issues and I'm not ...

The `@ViewChild` reference cannot be found

My main challenge is toggling a @ViewChild element using an *ngIf, followed by invoking a native event. This snippet shows my HTML element, tagged with #audioPlayer for extracting it through @ViewChild. <audio #audioPlayer *ngIf="convers ...

Angular aggregates information from numerous HTTP inquiries

I am looking to retrieve data for multiple ProductIds from a remote source using httpClient in an angular service. My goal is to merge the data within the service and then send it back. When working with just one productId, everything functions correctly: ...

Is it feasible to verify the accuracy of the return type of a generic function in Typescript?

Is there a way to validate the result of JSON.parse for different possible types? In the APIs I'm working on, various Json fields from the database need to have specific structures. I want to check if a certain JsonValue returned from the database is ...

Tips for denoting unnecessary non-null assertions in Typescript

Incorporated this wrapper (source) into the project I'm currently working on: export function expectToBeDefined<T>( arg: T, ): asserts arg is Exclude<T, undefined> { expect(arg).toBeDefined(); } The objective is to eliminate the usage ...

RxJS emits an array of strings with a one second interval between each emission

Currently, my code is set up to transform an Observable<string[]> into an Observable<string>, emitting the values one second apart from each other. It's like a message ticker on a website. Here's how it works at the moment: const ...

Is React 18 compatible with both react-redux and react-router?

At present, my react application is running on the following versions: react 17.0.x react-dom 17.0.x react-redux 7.2.x react-router-dom 5.x.x react-scripts 4.0.x redux 4.x.x My initial step towards upgrading to react@18 involved updating react-scripts to ...

encountering difficulties resolving dependency tree when attempting to generate a new Angular project

Today, I attempted to start a new Angular project using the command ng new <projectname>, but encountered the following error: npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: [email protected] npm ERR! Foun ...

Navigating in Angular is made easy with the Angular routing feature, which allows you to redirect

I have been working through the Angular Tour of Heroes Guide and encountered the section on the "default route". I decided to experiment by removing the pathMatch attribute from the Route associated with an empty string. Below is the code snippet in quest ...

Generate angular2 a Hashtable containing Strings and Lists

Having trouble printing a HashMap. Last week, I successfully resolved an issue with grouping two fields using Java 8. Now, I am faced with the challenge of printing the response of a project in angular2. The response that I am receiving can be found here ...

Angular's change detection is currently inactive

I need to toggle the visibility of a button based on the value of a boolean variable using the Output property. However, I am facing an issue where the button remains hidden even after the variable is updated with a true value. Parent Component.ts showE ...

Bringing Typescript functions into the current module's scope

Is it possible to import and reference a module (a collection of functions) in typescript without the need for the Module. prefix? For instance: import * as Operations from './Operations'; Can I access Operations.example() simply as example()? ...

Can an attribute be assigned to an Angular host element without specifying a value?

Exploring the concept of host binding on the input element's readonly attribute, aiming to add the attribute without assigning any value. Even though HTML specifications state that assigning a value may not make a difference as long as the attribute i ...

Exploring nested objects and arrays with Plunker - extracting their values

I have retrieved two arrays, each containing nested objects, from API endpoints. One array (preview) consists solely of numbers. For example: [{ obj1:[1, 2], obj2:[3, 4] }] To obtain strings associated with IDs, I made another call to a different en ...

I followed the step to download Angular using NPM Install

When attempting to work on a repository that uses Angular without having it installed on my machine, I ran npm i. However, Angular was not automatically installed. So, I had to separately install the Angular CLI before running ng serve --open ...

Experience the dynamic synergy of React and typescript combined, harnessing

I am currently utilizing ReactJS with TypeScript. I have been attempting to incorporate a CDN script inside one of my components. Both index.html and .tsx component // .tsx file const handleScript = () => { // There seems to be an issue as the pr ...

Is it possible to utilize a service that is already being used by the imported component?

Recently, I began working with Angular2 and have been quite impressed with it. However, today I encountered a challenge: I have a reusable alert component that utilizes its own service containing business logic. My question is, can I utilize the same serv ...

transferring attributes from a higher component to a lower one (modal)

I am relatively new to React and I want to share a detailed problem description: I have a Todo project that consists of multiple interfaces. The main interface displays all the lists, each containing a title, a group of tasks, and a button to create a ta ...