Unable to get the Angular Formly select option to bind

I'm currently working on binding formly select type options with the following code:

      fieldGroup: [
    {
      key: 'TimeOffTypeID',
      type: 'select',
      className: 'flex-40 padding-10',
      templateOptions: {
        label: 'نوع مرخصی',
        placeholder: 'نوع مرخصی',
        required: true,
        options: this.getTimeoffType,
        valueProp: 'TimeOffTypeID',
        labelProp: 'TimeOffTypeName',
      },

Additionally,

    types$: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);
     
    public get getTimeoffType(): Observable<any[]> {

    return this.types$.asObservable();
    }

and with DataService to retrieve data

      getTimeoffTypes() {

this.base
  .get(
    Controller.TIMEOFFTYPE,
    {
      TimeOffTypeID: 0,
    },
    Actions.SEARCH
  )
  .subscribe(({ result }) => {
    console.log(result)
    this.types$.next(result);
    
  })

}

The data retrieved seems correct, however it's not being bound to the form select options.

Answer №1

Dealing with the same issue, I couldn't see the options. By tweaking the code below, I managed to solve it: templateOptions : { label : "Gender.", labelProp : "name", valueProp : "value", options : [ { "name" : "Male", "value" : "male" }, { "name" : "Female", "value" : "female" }, { "name" : "Others", "value" : "others" } ], Note: I am utilizing Formly Material

Answer №2

Check out the code snippet below for a similar implementation:

data.service.ts

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class DataService {
    private data$ = new Subject<any>();

    sendData(value: any) {
        this.data$.next({ info: value });
    }

    clearData() {
        this.data$.next();
    }

    getData(): Observable<any[]> {
        return this.data$.asObservable();
    }
}

app.page.ts

import { Component, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';

import { DataService } from './_services/index';

@Component({ selector: 'app', templateUrl: 'app.component.html' })
export class AppComponent implements OnDestroy {
    dataValues: any[] = [];
    subscription: Subscription;

    constructor(private dataService: DataService) {
        // subscribe to component messages
        this.subscription = this.dataService.getData().subscribe( msg => {
          if (msg) {
            this.dataValues.push(msg);
          } else {
            // clear values on empty message
            this.dataValues = [];
          }
        });
    }

    ngOnDestroy() {
        // unsubscribe to avoid memory leaks
        this.subscription.unsubscribe();
    }
}

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

Trigger Angular Animation when there is a modification in the DOM element's appearance or styling

I've been working on implementing a fade-in animation in my Angular App that triggers every time the background changes, but I'm facing some challenges with it. Here's the relevant code snippet: HTML: <div @fadeIn [style.backgroundImag ...

What is the best way to trigger a function in React when a constant value is updated?

In my React application, I have 3 components. The parent component and two child components named EziSchedule and EziTransaction. Each component fetches its own data from an API. The data to display in the EziTransaction child component depends on the reco ...

Enhance your map by incorporating an overlay with ngx-openlayers

Currently, I am trying to implement zoom-in and zoom-out buttons on an OpenLayers map. I attempted to use the overlay method but encountered an error. Here is the code snippet for reference: zoom_button = document.getElementById('zoom') zo ...

Adapt button functionality according to selected dropdown value in Angular

I have implemented a License Key generation process in my application where user input is used to create a unique key that is then passed to the Java backend. The code snippet for generating the key is as follows: @PostMapping("/generate") public Li ...

After sending an HTTP Post request in Angular 5, users are required to click in order

Currently, I am in the process of developing an Angular 5 component that integrates "ngx-file-drop" and "ngx-spinner" modules. Upon uploading a file through my API, an issue arises. Despite the successful upload of the file and the console.log message disp ...

Angular2 - Issue with calling toPromise() method on this.http.get() function, as it is not recognized

I was following a tutorial on angular.io called Tour the Heroes, but instead of sticking to the tutorial I decided to make a real GET request for some JSON data. Here is a snippet of my code: private userUrl = 'https://jsonplaceholder.typicode.com ...

The property '.....' is missing an initializer and has not been explicitly assigned in the constructor

I want to address an issue with a similar question title that was asked 5 years ago on Stack Overflow. The problem is related to declaring a variable as an array of a specific object type in an Angular component using TypeScript 4.9. Even though I tried t ...

Exploring how to iterate through multiple arrays simultaneously in JavaScript

I have a specific problem with processing two sets of array objects to achieve a desired output. var parts={"Glenn": [12,22,32], "Ryan": [13,23,33], "Steve K": [14], "Jerom":[15,25,35], }; var labor={ "Glenn": [12,22,32], "Ryan": [13,23,33], "Steve K": [ ...

Adding a blank line in an Angular table using primeNG to display database information requires a specific method

In my Angular application, I am utilizing primeNG table to display data fetched from a database. The table comes with 'add' and 'delete' buttons for user interaction. To view the interface, click on this link: https://i.stack.imgur.com/ ...

What causes the cursor in an editable div to automatically move to the front of the div?

<div className="min-w-[600px] min-h-[36.8px]" > <div id={`editableDiv-${Object.keys(item)}-${index}`} className="p-3" contentEditable suppressContentEditableWarning onInput={(e) => onChange(e)} > ...

Installing a 3rd party plugin in Angular using the Angular-cli tool

When attempting to install angular2-grid on my Angular-cli with version 2.0.0-rc.1, I followed the tutorial exactly as described but encountered an error. zone.js:101 GET http://localhost:4200/node_modules/angular2-grid/dist/NgGrid 404 (Not Found)sche ...

Validating Form Controls with Dynamic Names in Angular 5

Initially, I believed this task would be straightforward. The following snippet of HTML is functioning as anticipated: <label class="mb-0 form-label"> Doc Part </label> <input type="number" name="DocPart" #DocPart="ngModel" class="form- ...

What is the best way to transfer information between sibling child components under the same parent in Angular 4?

I am dealing with a parent component A that has two child components B1 and B2. B1 interacts with an API to fetch some data which needs to be consumed by B2. Is there a way to directly pass the data from B1 to B2? If not, what is the best method to transf ...

What are the steps to publish an Electron application for Windows, Mac, or Linux operating systems?

After developing an App using Angular 2, I successfully created iOS and APK files with some modifications using Ionic. Now, I am looking to create a desktop app file for the same project. I have researched various resources on Electron but haven't f ...

What is the proper way to utilize a custom property that has been incorporated into my Pinia stores in a Typescript project?

Currently utilizing Vue 3 alongside Pinia; my api service is utilized for making requests to the api. I have included it as a property to ensure availability across all stores: In my main.ts file: import { http } from "@/services/http"; const s ...

Using Rails 5 API to generate a new object using JSON with nested resources

Here is the JSON data that was received as parameters from an external Angular web app: { "provincia": { "id": 1, "name": "Province" }, "username": "tester", "direccion": "new avenue 100", "email": "<a href="/cdn-cgi/l/email-protectio ...

Having trouble with obtaining real-time text translation using ngx translate/core in Angular 2 with Typescript

Issue : I am facing a challenge with fetching dynamic text from a JSON file and translating it using the translate.get() method in Angular2. this.translate.get('keyInJson').subscribe(res => { this.valueFromJson = res; /* cre ...

Angular component name constraints - 'the selector [your component name] is not permissible'

When trying to generate a component using the Angular 6 CLI (version 6.0.7), I encountered an issue. After typing in ng g c t1-2-3-user, I received an error message stating that the selector (app-t1-2-3-user) is invalid. I wondered if there was something ...

ag-grid angular - update group row when selection changes

After thoroughly reviewing the documentation and API references, I have not found a method to initiate a refresh on a group row when a selection change is made to its children. In my current project using ag-grid in Angular 7, I am utilizing a custom rend ...

When you click, you will be directed to the specific details of the object

I have a recipe component that displays a list of recipes from my database and a recipe-detail component that should show the details of a selected recipe. What I aim to achieve is that when someone clicks on a recipe name, they are routed to the recipe-de ...