Exploring Angular 10 Formly: How to Retrieve a Field's Value within a Personalized Formly Wrapper

Utilizing Angular 10, I have a formly-form with a select-field named session. This select field provides options from which to choose a dndSession. Each option holds key-value pairs within an object.

I want to add a button next to the select-field that triggers a modal-component called app-session-update-modal. My plan involves creating a custom field-wrapper, named session-edit-wrapper, and assigning it to the wrapper option of the session's FormlyFieldConfig. The goal is to link the currently selected dndSession object to the app-session-update-modal-component for editing. This is where I am encountering difficulties.

The challenge lies in accessing the value of a field within a wrapper (specifically the ng-container fieldComponent) to bind it to an attribute on my modal-component. Are there any events that I can leverage for this purpose? (I have attempted using ngModelChange without success as shown below)

To achieve the desired outcome, I understand that I need to connect the dndSession-objects as a Subject<dndSession> (from rxjs). However, establishing this connection has proven to be challenging.

Displayed below is the current HTML structure of my Wrapper, which admittedly contains errors. At this stage, I am merely speculating on what the correct Syntax should be. Ultimately, my objective is to bind a Subject<dndSession>-object to the session_subject attribute of my modal component:

//session-update-wrapper.html
<div class="d-flex">
    <div class="flex-1">
        <ng-container #fieldComponent (ngModelChange)="sessionPkSubject.next(#fieldComponent.value)"></ng-container>
    </div>
    <div class="ml-2">
        <app-session-update-modal [session_pk_subject]="sessionPkSubject"></app-session-update-modal>
    </div>
</div>
//session-update-wrapper.ts
... //Skipped over imports and the @Component decorator
export class SessionUpdateWrapperComponent extends FieldWrapper implements OnInit, OnDestroy{
  sessionPkSubject : Subject<number>;

  ngOnInit(): void{
    this.sessionPkSubject = new Subject();
  }

  ngOnDestroy(): void{
    this.sessionPkSubject.complete();
  }
}

Answer №1

After stumbling upon this helpful response from @a.aitboudad in an unexpected context and reviewing the ensuing comments, I was able to deduce that component wrappers provide access to a variety of variables, including formControl. This access is likely inherited from its class structure. For more details on available variables, refer to this github page.

By leveraging the formControl of the select field, we can extract its Observable formControl.valueChanges to track the selected value changes over time through subscription.

//session-update-wrapper.ts
export class SessionUpdateWrapperComponent extends FieldWrapper {}
//session-update-wrapper.html
<div class="d-flex">
    <div class="flex-1">
        <ng-container #fieldComponent></ng-container>
    </div>
    <div class="ml-2">
        <app-session-update-modal [session_pk_observable]="formControl.valueChanges"></app-session-update-modal>
    </div>
</div>

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

Injecting services and retrieving data in Angular applications

As a newcomer to Angular, I am trying to ensure that I am following best practices in my project. Here is the scenario: Employee service: responsible for all backend calls (getEmployees, getEmployee(id), saveEmployee(employee)) Employees components: displ ...

Incorporating Scatter Dots into a Horizontal Stacked Bar Chart using Chart.js

My horizontal stacked bar chart is not displaying pink scatter dots based on the right y axis numbers. I need help figuring out what I am doing wrong. When I change the chart to a normal bar instead of horizontal, the dots appear. However, I require them ...

Using Selenium WebDriver with JavaScript: Handling Mouse Events (mouseover, click, keyup) in Selenium WebDriver

I am currently working on integrating Selenium testing for mouse events with dynamically generated elements. Specifically, I am attempting to trigger a "mouseover" event on an element and then interact with some icons within it. However, I have encountere ...

The parameter 'CallHistoryMethodAction<[string, unknown?]>' does not match the 'UserActionType' parameter

Using a development environment with React, TypeScript, and connected-react-router. My Intention: I aim to utilize the router action asynchronously within the user action. After successful login, I want the application to navigate to the main screen. Err ...

Creating an Angular Form Array with Radio Buttons

Having difficulties with implementing reactive forms and an array of radio buttons. Here is a glimpse at my form structure: https://i.sstatic.net/C7t2c.png Each row contains player details and I need to select the status for each player. Component sn ...

Angular SignalR template for ASP.NET Core 6

Is there a specific .NET template for an operational ASP.NET Core 6 application with SignalR and an Angular ClientApp using WebSockets transport? I've managed to only make the ServerSideEvents transport function. The 'dotnet new angular' co ...

Having difficulty selecting an item from the MaterialUI package

While trying to utilize the MaterialUI Select component with typescript/reactjs, I encountered an issue during the instantiation of the Select element. The error message I'm receiving states: Type 'Courses' is missing the following properti ...

Code in Javascript to calculate the number of likes using Typescript/Angular

I have encountered a challenge with integrating some JavaScript code into my component.ts file in an Angular project. Below is the code snippet I am working on: ngOninit() { let areaNum = document.getElementsByClassName("some-area").length; // The pr ...

I keep running into an issue where Angular2 and lodash just cannot seem to

Why is there so much commotion around Angular2 and lodash? It seems unnecessary... Here is how I typically install: npm install --save lodash npm install --save @types/lodash UPDATE: After following a blog post on fixing angular2-and-lodash-cannot-find ...

The `setState` function is failing to change the current value

I'm having an issue with setting State in the dropdown component of semantic-ui-react while using TypeScript in my code. The selected category value is always returning an empty string "". Any suggestions on how to resolve this problem? impo ...

Using Angular frontend to access Django Admin

Is it possible to integrate Django admin with an Angular frontend? I'm currently using Angular version 8.0 for the frontend and Django for the backend. In my urls.py file, I have added the admin as shown below: from django.urls import path, re_path f ...

Angular 5 - Inconsistent page refreshing when navigating instead of real-time DOM updates

I recently started a brand-new Angular 5 project and set up routing by following the instructions in the Angular Documentation. However, I encountered an issue where instead of updating the DOM, the page was refreshing when navigating between routes. Here ...

Incorporating ng2-bootstrap into an Angular2 project with SystemJS starter template

I am attempting to integrate the ng2-bootstrap modal functionality into a component of my project that is built using the angular2-starter. Below is my systemjs.conf.js configuration: /* * This config is only used during development and build phase onl ...

What is the best way to modify the class of specific items within an ngFor loop in

I apologize if this question has already been addressed, but I couldn't find a suitable solution. My goal is to develop a basic chat application where messages sent by the current user are displayed on the right side of the screen, while messages from ...

What is the best way to transfer user input as a key value or variable from an HTML form to an Express.js application and then

Is it possible to dynamically pass user input as the key value? For example, instead of /hand-hold?handhold=userinput, I want the value entered by the user each time to be used. Any assistance on this matter would be greatly appreciated. app.component.ts ...

What is the best way to modify the KeyName in an object?

Having difficulty parsing an Object by changing keynames due to the error message "Element implicitly has an 'any' type because expression of type 'keyof SignInStore' can't be used to index type '{}'". interface SignInSto ...

Is it possible to lengthen a function in TypeScript?

I am part of a team responsible for maintaining a unique JavaScript library that generates spy functions. These spy functions are designed to allow users to monitor how a function is called, primarily used in the context of unit testing. Our library creat ...

What is the process for importing a TypeScript module exclusively through typings without having to download it separately?

Currently, I am working on a widget for a website that is already utilizing jQuery and I am using TypeScript. The goal is to embed my output into the host website while taking advantage of the existing jQuery library loaded by the host site. In order to r ...

A generic type in TypeScript that allows for partial types to be specified

My goal is to create a type that combines explicit properties with a generic type, where the explicit properties have priority in case of matching keys. I've tried implementing this but encountered an error on a specific line - can anyone clarify why ...

How can Angular CLI add extra static resources while live reloading?

Currently, I am utilizing the most recent version of the Angular CLI. The issue I'm facing involves mock http calls that reference local JSON files such as '../app/myfile.json'. When I reload the application, I consistently encounter 404 err ...