Dropdown box not displaying any choices

I developed a basic reusable component in the following way:

Typescript (TS)

import {Component, Input, OnInit} from '@angular/core';
import {FormControl} from '@angular/forms';

@Component({
  selector: 'app-select',
  templateUrl: './select.component.html',
  styleUrls: ['./select.component.css']
})
export class SelectComponent implements OnInit {
  @Input() control: FormControl;
  @Input() label: string;
  @Input() options: [];
  @Input() idAndForAttributes: string;
  @Input() customClass: string;

  constructor() { }

  ngOnInit() {
  }
}

HTML

<div class="form-group" [ngClass]="{'invalid': control.invalid && control.touched && control.dirty}">
  <label [attr.for]="idAndForAttributes">{{ label }}:</label>
  <select class="form-control" [ngClass]="customClass" [formControl]="control" [attr.id]="idAndForAttributes">
    <option value="0">- Select -</option>
    <option *ngFor="let item of options" [ngValue]="item.id">{{item.description}}</option>
  </select>
  <ng-container *ngIf="control.dirty && control.touched && control.invalid">
    <div *ngIf="control.errors.required || (control.errors.min && control.value == 0)">
      <small style="color: #c62828;">
        Value is required.
      </small>
    </div>
  </ng-container>
</div>

Currently, my goal is to utilize this component within another html file by using the code below:

   <form [formGroup]="profileActivityForm">
     <app-select [control]="profileActivityForm.get('activityType')" [idAndForAttributes]="'type'" [label]="'Type'"
                [options]="profileActivityTypes"></app-select>
   </form>

In the corresponding Typescript file (TS), I have:

 profileActivityTypes: string[] = [];

  ngOnInit() {
    this.profileActivityTypes.push('New')
    this.profileActivityTypes.push('Update')

 this.profileActivityForm = this.fb.group({
 activityType: [0]
    });
  }

However, there seems to be an issue where invisible options are being displayed as shown in this picture.

I suspect the problem lies within the HTML code of the reusable component:

<option *ngFor="let item of options" [ngValue]="item.id">{{item.description}}</option>

The error might occur due to it trying to display a description. How can I send the items as descriptions from the child component?

UPDATE

I attempted:

  profileActivityTypes: [] = [];
  ....

let profileActivities = [{ description: 'New' }, { description: 'Update' }]
this.profileActivityTypes.push(profileActivities)

Nevertheless, it throws an error upon pushing:

Argument of type '{ description: string; }[]' is not assignable to parameter of type 'never'

Answer №1

To find a solution, I decided to modify the assignment of the profileActivities array instead of creating it separately and then pushing items into it. The new approach involves directly assigning it as follows:

 profileActivityTypes = [];
   this.profileActivityTypes = [{ id: 1, description: 'New' }, {id: 2, description: 'Update'}]

I trust that this alternative will be beneficial for a wider audience!

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

Customized length limits in Vue components

In the form, there is a field for the phone prefix and another one for the actual phone number. The validation needs to be dynamic, with the minimum length calculated using the formula 7 - phoneprefix.length and the maximum length as 15 - phoneprefix.lengt ...

I encountered a SyntaxError while parsing JSON due to an absence of a number after a minus sign at position 1

I am trying to use the replicate model visoar/product-photo:edf42659dae0da88a26dba4912e7e4bb6c2fba25b1e1c6a5464cf220e467bce0, but when I provide it with an image and a prompt like on this page.tsx: "use client" import { LandingNavBar } from &apo ...

Tips for implementing the same autocomplete feature across multiple form fields

Struggling to add multiple products and provide auto-suggest for each product name? You're not alone. It seems like the auto suggest feature is only working for the first product. Can anyone point out what's going wrong here? Here's my HTML ...

What is the best way to extract data from Azure Data Lake and showcase it within an Angular-2 interface?

I am facing a challenge where I need to retrieve files with content from Azure Data Lake and display them in an Angular-2 Component. The issue is that when using the List File Status() method, I am only able to obtain file properties, not the actual conten ...

Modify the color scheme of an HTML webpage

Looking to enhance my HTML page with a new feature. The page is responsive and currently using Bootstrap CSS and JS. My goal is to allow users to change the color of the page dynamically by selecting a background or text color. Check out this example link ...

What is the best way to position a material icon to the right of an input field?

Currently, I am using a mat auto complete text box with a search icon that is aligned to the left of the input. I am looking to align this search icon to the right instead. Can anyone provide assistance on how to achieve this? <mat-form-field floatLa ...

Guide to creating content on an NFC tag with Ionic

I am struggling with my button calling the test2 function and the code I have is not working as expected. Here is what I currently have: import { Component } from '@angular/core'; import { NFC, Ndef } from '@ionic-native/nfc/ngx'; @Com ...

Sending data to API using AngularJS Http Post

Upon clicking "Add new User", a modal pop-up will appear with a form containing a text field and a checkbox. However, upon clicking the create button, the data is not being posted to the API and the modal pop-up remains open without closing. I would like ...

Stuck with the same icon even after a successful AJAX call

I am currently working on implementing a 'add to my list' function in my web app. The goal is to change the color of an icon when a user clicks on it, after sending the necessary data to the server. Despite successfully sending the data to the s ...

How come these functions continue to be executed asynchronously even when the Async module is being utilized?

My decision to utilize the Async module aimed at populating a mongodb collection according to a specific order proved to be quite challenging. Despite the fact that the code worked without Async, it failed to insert documents in the desired sequence: func ...

AngularJS: Issue with scope not updating across several views

Having one controller and two views presents a challenge. ClustersController angular.module('app.controllers').controller('ClustersController', [ '$scope', 'ClustersService', function($scope, ClustersService) { ...

Mastering the art of manipulating arrays with jquery

Trying to implement a dynamic search bar feature using jQuery. Here is the code snippet: Fiddle : https://jsfiddle.net/fpLf1to4/ var inputSearch = $('.searchInput').hide(); var searchArray = ['s','e','a',& ...

Import a picture file into Photoshop and position it at a designated location

I need assistance with developing a function that can load an image and position it at specific x, y coordinates in Photoshop. Below is the code I have so far: var docRef = app.activeDocument; function MoveLayerTo(fLayer, fX, fY) { var Position = fLaye ...

"jQuery's .each() method is only iterating through the last element in

I am encountering an issue with this function not operating correctly... only the last Element shows the box. NOTES: <aside> is set to position: fixed; and I understand that this is not the "correct" use of <article> tags, but it helps me dist ...

Is there a way to define an interface that consists of child objects as the type for a function that uses destructured props?

Is there an alternative to this method? import { client } from "./setupApi"; export const getLayout = ({ page, entity, list }: {page: string, entity: string, list: string}) => { return client.get("/secure/nav.json"); }; How do I ...

Calculating the total value in a Laravel database

Is there a way for me to calculate the number of apartments in a project based on the floor they are located on? This is db $table->id(); $table->unsignedBigInteger('project_building_id')->nullable(); $table->unsignedBigInteger(&apos ...

Using a .NET Web-API controller variable as a parameter in a JavaScript function

I am looking to send a "variable" from the controller to a JavaScript function. The code I have implemented is as below: <div ng-controller="faqController"> <div ng-repeat="c in categories"> <h2 onclick="toggle_visibility(&apos ...

Debugger for Visual Code unable to locate URL in Microsoft Office Add-in

I recently installed the Microsoft Office Add-in Debugger VS code extension, but I'm having trouble getting it to work despite following the instructions. Every time I try, an error message pops up: Upon inspecting my launch.json file, I found this U ...

Tips for optimizing iframe loading times using the onload event

I am facing an issue with the timing of iframe loading while using a list of URLs as sources. I have created a child iframe and appended it to the DOM, then run the onload function for further processing. However, the time recorded for each iframe seems in ...

Vue (Gridsome) App encountering 'Cannot POST /' error due to Netlify Forms blocking redirect functionality

Currently, I am in the process of developing my personal website using Gridsome. My goal is to incorporate a newsletter signup form through Netlify Forms without redirecting the user upon clicking 'Submit'. To achieve this, I utilize @submit.prev ...