Tips for effectively utilizing ng-multiselect-dropdown without the need for [(ngModel)]

Using the ng-multiselect-dropdown in Angular 14, I encountered an issue when trying to use singleSelection without initial data (without [(ngModel)]) to display a placeholder. Here is my code snippet:

<ng-multiselect-dropdown
     [placeholder]="'search the country'" [settings]="dropdownSettings"
     [data]="dropdownList">
</ng-multiselect-dropdown>

This error occurred when selecting an option from the dropdown:

https://i.sstatic.net/l2Inv.png

If anyone can provide assistance, it would be greatly appreciated.

Answer №1

To implement this functionality with Angular Reactive Form, you can wrap it as shown below:

HTML:

<form [formGroup]="myForm">
    <ng-multiselect-dropdown
        name="city"
        [placeholder]="'Select City'"
        [data]="cities"
        formControlName="city"
        [disabled]="disabled"
        [settings]="dropdownSettings"
        (onSelect)="onItemSelect($event)">
    </ng-multiselect-dropdown>
</form>

Typescript:

import { FormBuilder, FormGroup } from '@angular/forms';
import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'multiple-demo',
    templateUrl: './multiple-demo.html'
})
export class MultipleDemoComponent implements OnInit {
    myForm:FormGroup;
    disabled = false;
    ShowFilter = false;
    limitSelection = false;
    cities: Array = [];
    selectedItems: Array = [];
    dropdownSettings: any = {};
    constructor(private fb: FormBuilder) {}

    ngOnInit() {
        // Initialization of cities and settings
    }

    onItemSelect(item: any) {
        console.log('onItemSelect', item);
    }
    
    // Other event handlers and functions
    
}

For a live demo and more information, check out their official website here.

Hopefully, this solution will meet your requirements.

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

Displaying Mat-error from response in Angular and Django user authentication process is not functioning as expected

Currently, I am working on the Signup page and facing an issue with handling 'if username already Exists'. I have successfully logged the Error message I want to display in the Console but it is not appearing on the Mat-error Label. This snippet ...

Ways to determine the presence of a route in Angular2

How can I determine if a route exists in Angular2? I have a function that stores the route to navigate to if the user is unauthorized. Upon logging in, I use: this.router.navigate([this.authService.redirectUrl]); However, I only want to navigate IF the ...

Tips for modifying the data type of a property when it is retrieved from a function

Currently, I have a function called useGetAuthorizationWrapper() that returns data in the format of { data: unknown }. However, I actually need this data to be returned as an array. Due to the unknown type of the data, when I try to use data.length, I enc ...

Angular pipe with Observable request to filter HTML content

Async pipes are commonly used to subscribe to observables and receive updated values. However, I am in the process of creating a custom pipe that translates a classification using its code asynchronously. Is there a way to wait for the async action to comp ...

How can I implement set/get/destroy methods in fastify-session to effectively save the session data in MongoDB?

I am working with TypeScript and trying to find a way to store sessions in mongoDB rather than in memory. After reading the documentation, I still couldn't figure it out (https://github.com/SerayaEryn/fastify-session). Can anyone guide me on how to ac ...

I find that the value is consistently undefined whenever I attempt to set it within a promise in Angular

Hi there, I've encountered an issue with my getData() function in accountService.ts. I'm attempting to fetch user data and user account data simultaneously using a zip promise. Although the resolve works correctly and I receive the accurate data, ...

Why is my Typescript event preventDefault function ineffective?

Despite all my efforts, I am still unable to prevent the following a tag from refreshing the page every time it's clicked. <p> <a onClick={(e) => handleClick} href="&qu ...

Navigating through pages in your IONIC 3 application: A guide to routing

I have been working on an Ionic 3 project and currently using NavController for page navigation. For example: this.navCtrl.push(DetailsPage); However, I now need to switch to Angular routing. I came across a similar query regarding Ionic 2 in this link. ...

How can RootStateOrAny be turned off in React with typescript?

Whenever I need to employ useSelector, I find myself repeating this pattern: const isLoading = useSelector( (state) => state.utils.isLoading ); Is there a shortcut to avoid typing out RootStateOrAny each time? It's starting to become a hassl ...

Can anyone suggest a more efficient method for specifying the type of a collection of react components?

Picture this scenario: you are extracting data from an API and creating a list of Card components to be displayed in a parent component. Your code might resemble the following: function App() { let items = [] // How can I specify the type here to avoid ...

Creating a TypeScript module declaration for exporting a class

Just starting out with TypeScript and running into issues while trying to load an es6 javascript module. Here's the code snippet from my javascript file: //TemplateFactory.js export class TemplateFactory{ static getTemplate(module){ } } In ...

The delete() method in AngularFirestoreDocument does not actually remove documents from the database

My function for removing a document in Firestore doesn't seem to be functioning correctly. I've double-checked the document ID it receives, but that doesn't appear to be the issue. I even tried removing the return statement before the delet ...

The issue of Angular finalize not functioning when used within a pipe alongside switchMap

Once the user confirms, I want the process to begin and keep the modal busy until it's complete. However, the current code does not function in this manner. The isModalBusy condition only turns false when an HTTP error is returned from the service. In ...

Can JavaScript code be utilized within Angular?

Hello, I am wondering if it is acceptable to include JavaScript code within Angular for DOM manipulation purposes. Here is a sample of what I have in mind: document.querySelectorAll('div.links a').forEach(function(item) { const tag = window.lo ...

Replacing child routes with an Angular wildcard route

I'm facing an issue with integrating a module named "host" into the app-routing.module. The problem I encountered is that the wildcard route takes precedence over loading the Host component, leading to the display of the PageNotFoundComponent instead. ...

Error occurred in child process while processing the request in TypeScript: Debug Failure encountered

I encountered the following error in TypeScript while running nwb serve-react-demo: Child process failed to handle the request: Error: Debug Failure. Expression is not true. at resolveNamesWithLocalCache (D:\Projects\react-techpulse-components& ...

Problem integrating 'fs' with Angular and Electron

Currently, I am working with Angular 6.0, Electron 2.0, TypeScript 2.9, and Node.js 9.11 to develop a desktop application using the Electron framework. My main challenge lies in accessing the Node.js native API from my TypeScript code. Despite setting "com ...

the data chart is malfunctioning

Is it possible to use a chart with Angular that dynamically retrieves data from an API? I am able to get the data from the API in ngOnInit, but encounter difficulties assigning it to the chart. Below is the component TS code : import { Component, OnInit, ...

Angular consistently marks form controls as mandatory, even in the absence of validators

Recently, I have been working on this code snippet where I make use of a deepCopy function to ensure that I avoid any unexpected pass by reference issues. const formGroup = this.allowances() formGroup.removeControl('allowanceEndDate') con ...

I am encountering an Ionic issue where the Footer selector fails to work properly

I am working on my Ionic page and I have created a separate component for the footer. However, when I use the footer selector in my page, an error is displayed. This is the code for my "foot-card" component (foot-card.ts): import { Component } from &apos ...