View choices in a dropdown menu

Looking to display a list in a dropdown using Angular. Here's what I've tried:

TypeScript:

Constructor

export class Merchant {
  constructor(
    public id: string,
    public name: string
  ) {}
}

Component:

import {Merchant} from "../domain/merchant";
import {MerchantService} from "../service/merchant.service";

@Component({
  selector: 'app-terminal',
  templateUrl: './terminal.component.html',
  styleUrls: ['./terminal.component.scss']
})
export class TerminalComponent implements OnInit {    
 . ..........
 constructor(private terminalService: TerminalService,
              private merchantService: MerchantService,
              private router: Router,
              private route: ActivatedRoute) {
  }      
  merchants: Merchant[];     
  ngOnInit() {       
    this.route.params.pipe(
      flatMap(params => {
        if (params['id']) {
          return this.terminalService.get(params['id']);
        } else {
          return of(null);
        }
      })
    ......   
  }    
}

HTML code:

<div class="form-group type">
    <div class="input-group-prepend">
      <label for="type">Merchant</label>
    </div>
    <select class="custom-select" name="type" [(ngModel)]="terminal" id="type" required>
      <option selected></option>
      <option [value]="type" *ngFor="let merchant of merchants">{{merchant.name}}</option>
    </select>
  </div>

The dropdown is showing up empty. Can anyone pinpoint where the issue might be?

Answer №1

When iterating through options in an empty merchants array without initialization, there will be no items to select from. For example:

merchants: Array<Merchant> = [{name:'test' , id:'test'}];

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

Tips for identifying when a div reaches the top or bottom of the browser window in Angular 4

This question has solutions available for JQuery (which I prefer not to use in the current project) and Angular1 (which I am unfamiliar with). ...

Are the module options in tsconfig referring to the 'SystemJS' module loader system?

When configuring a tsconfig.json file, one option that can be entered as the value for the compilerOptions 'module' property is: System This results in the following format: { "compilerOptions": { "module": "System", ... Is the &ap ...

When using Angular's auxiliary routes, everything works smoothly in a local environment. However

I'm encountering an issue with accessing an Auxiliary route directly. Everything works fine when I access /home/(post:12) locally, but once the app is deployed to GitHub pages, I'm getting a wild 404 error. The auxiliary route is responsible for ...

Updating Angular Material theme variables during the build processIs this okay?

How can I easily customize the primary color of my angular 6 application to be different for development and production builds? Is there a simple solution to automatically change the primary color based on the build? ...

Implementing TypeScript for augmented styling properties in a component - a guide

I have custom components defined as follows: import React from 'react'; import styled from '../../styled-components'; const StyledInput = styled.input` display: block; padding: 5px 10px; width: 50%; border: none; b ...

JavaScript's Array.map function failing to return a value

Here is a snippet of code from an api endpoint in nextJS that retrieves the corresponding authors for a given array of posts. Each post in the array contains an "authorId" key. The initial approach did not yield the expected results: const users = posts.ma ...

Downcasting on "this" is not supported in Typescript

In the example below, the TypeScript compiler does not allow for a direct cast of this to Child. However, it is possible to achieve this using an intermediate variable like 'temp' or double casting as shown in the commented lines. Is this behavio ...

Guide on encoding base64 within an Azure DevOps Pipelines extension

I'm in the process of creating an Azure Pipelines extension using Typescript and referring to Microsoft's documentation During my development, I encountered an issue when trying to base64 encode a string using the btoa() function which resulted ...

A guide on implementing JSON data projection with TypeScript

{ "ClaimType": ["The 'Claim Type' should have a minimum length of 4 characters. You have only entered 2 characters."], "ClaimValue": ["The 'Claim Value' should have a minimum length of 4 characters. You have only entered 1 chara ...

The pathway of external component template or css during the TypeScript compilation process to the outDir destination

When developing Angular 2 applications, I often run into an issue with my tsconfig.json file. In this file, I have set the parameter as follows: "outDir": "dist" This configuration instructs the TypeScript-to-JavaScript compiler to save the compiled file ...

What is the proper way to utilize e.key and e.target.value in TypeScript when coding with React?

I'm struggling to get e.key and e.target.value working with the following code: const handleInputKeyPress = (e: React.KeyboardEvent<HTMLInputElement> ) => { if(e.key ==='Enter') { dfTextQuery(e.target.value) } }; Why is & ...

Combining switch statements from various classes

Is there a way to merge switch statements from two different classes, both with the same function name, into one without manually overriding the function or copying and pasting code? Class A: protected casesHandler(): void { switch (case){ ...

What is the method for determining the width of a Mat-Table once it has been displayed?

When utilizing Angular Material Mat-Table in conjunction with Angular 8, I am passing the dataSource dynamically. The number of rows and columns varies each time. Is there a method to calculate the width of the table once it is rendered on the screen? &l ...

Tips for managing and identifying canceled requests in an Angular HTTP interceptor

Having trouble handling cancelled requests in my http interceptor. Despite trying various methods from SO, I can't seem to catch it. Here is an example of how my interceptor looks: public intercept(req: HttpRequest<any>, next: HttpHandler) { ...

Angular Error Handler - Extracting component context information from errors

In the event of a TypeScript error, I am looking to send all of the component's attribute values to a REST API. It would greatly benefit me if I could access the component's context (this) in order to retrieve the values of all attributes within ...

Issue R10 (Start-up delay) -> Failure of web application to connect to $PORT in the given 60 seconds after being launched (Angular)

I am currently in the process of building an Angular 7 application and attempting to connect it to Heroku (I am fairly new to using Heroku). Upon trying to run the application on Heroku, I encountered the following error: https://i.stack.imgur.com/ySmJw.p ...

Different ways to prevent invalid entries in an input field with type datetime-local

<input type="datetime-local" step="1"> Is there a way to prevent invalid date input? For example, entering a date like "11:11:1111" in the format "mm-dd-yyyy". How can this be addressed using Moment.js? ...

Transform an Array<string> into an object with the strings of the array as keys

I am attempting to develop a type conversion method that transforms an array of strings into an object with the string values as keys. type ObjectFromKeys<T extends Array<string>> = { [K in keyof T]: string } declare const o: ObjectFromKeys& ...

The 'status' attribute is not found within the 'User' data type

Currently, I am invested in a personal project using Angular 6 for the client side and Laravel 5.6 for my API. Despite successful testing of the API endpoints with Postman, I find myself struggling to comprehend the behavior of my auth service. To provide ...

Maintain Angular Dropdown Menu Open Across Page Refresh

I am currently working on an HTML/Typescript view that is connected to a SQL Database. Whenever there are changes made to the database, the entire webpage reloads. The issue we are facing is that we have dropdown menus on the page that clients want to rema ...