Angular 2: Implementing functionality on selected option

I have written code for my HTML

<select [(ngModel)]="empfile.storeno"  class="form-control">
    <option *ngFor="let p of concept" [ngValue]="p.MAP_Code">{{p.MAP_Code}}</option>
</select>

As for my component

public concept = [];
public datakartu: any = {cardno:""};
public empfile : any = {fullname:"",nik:"",disclimit:"",birthday:"",email:"",storeno:""};

constructor(
    private _router: Router,
    private _httpprovider: Httpprovider
) {}

ngOnInit() {

}

cardnof(newValue) {
    this.datakartu.cardno = newValue
    this._httpprovider.httpReq('http://192.168.1.40:5000/getdataemp','POST',{cardno:newValue},null).subscribe((data)=> { 
        var rData = [];
        for (let i=0;i<data.length;i++) {
            rData.push(data[i]);
        }

        var store = rData.map(i=>(i.Store_ID)).toString();

        this.empfile.storeno = store; << this line updates the display of the combobox

console.log(this.empfile.storeno); << here the console log is displayed
    });
} 

I added this.empfile.storeno = store; to change the combobox display based on database values, but it shows up blank.

However, when I use console.log(), the value is there but it doesn't display on the screen.https://i.sstatic.net/HOorj.png

Basically, I want to fill in other fields with data from the database after entering the card number, but the combobox isn't displaying anything. The console log does show the value.

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

Here is the result when I console log:

this.empfile.storeno = store;

Answer №1

If you want to change the display on a combobox, make sure to set [(ngModel)] to an object rather than just a property of the object. Using a different object or literal with the same value will not work; it needs to be an object from the same collection.

var store = rData.map(i=>(i.Store_ID)).toString();

var filteredValues = concept.filter(i => i.MAP_Code === store);
 
if(filteredValues.length > 0) {
   this.empfile = filteredValues[0];
}

Also remember to set ngModel to empfile in the html



<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code><select [(ngModel)]="empfile"  class="form-control">
    <option *ngFor="let p of concept" [ngValue]="p">{{p.MAP_Code}}</option>
</select>

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

Understanding the contrast between .value and .setValue within reactive forms in Angular versions 7 and higher

Can you help me distinguish between .value and .setValue in Angular 7+? Under what specific circumstances would it be appropriate to use .value or .setValue? For example, in this scenario: this.createLocForm.setValue(data); And in another case, like this ...

When trying to reference "bootstrap" from the "node_modules" in my "angular.json" file, I encountered an issue where the styles were not applying as expected in a specific example

Recently, I developed a fresh Angular application and integrated Bootstrap and jQuery. However, upon testing the app with a simple button example, it doesn't appear to be functioning as expected. https://i.sstatic.net/YIiHd.png Shown above is the An ...

Having trouble directing my attention towards a Mat card when using viewchildren in Angular

My current challenge is trying to focus on a specific card from a list of mat cards Despite my efforts, I keep encountering an error that reads: Cannot read property 'focus' of undefined Access the code on StackBlitz The desired functionali ...

Is there a way to temporarily pause HTTP calls?

Looking for a solution to this problem I'm facing with the code below: while (this.fastaSample.length > 0) { this.datainputService .saveToMongoDB(this.fastaSample.slice(0, batch)) .subscribe(); } The issue is that I can't send all ...

Resolve the issue of text overflow in PrimeNG Turbo Table cells

When utilizing Primeng table and enabling the scrollable feature, the table is expected to display a scrollbar while ensuring that the text within the cells does not overflow. Unfortunately, this expected behavior is not occurring. To see an example of th ...

React Material-UI - Mobile-friendly Masonry Layout

I'm new to working with React and I'm currently exploring how to create a Masonry image grid that is responsive for mobile devices. The goal is to have the images stack on top of each other in mobile view while maintaining the classic Masonry gri ...

Has anyone discovered an Angular2 equivalent to $provide.value() while testing promises?

I am currently experimenting with testing a promise in Angular2. Here is what I have so far: this.fooService.getData().then(data => this.fooData = data); If you are interested in learning about testing promises for Angular 1, check out this article. ...

The ngOnChanges lifecycle hook is triggered only once upon initial rendering

While working with @Input() data coming from the parent component, I am utilizing ngOnChanges to detect any changes. However, it seems that the method only triggers once. Even though the current value is updated, the previous value remains undefined. Below ...

When Angular 5 is loaded, the select list on the page will automatically display the matching option that

I am currently working on a form that is read-only and showcases data retrieved upon loading the page. One of the sections in this form includes an IsActive dropdownlist with options True or False. I have set up my model property isActive to bind with the ...

What is the reason that setState functions properly when parsing each key separately, but fails when passed as an object?

Currently, I am delving into the world of React and TypeScript, but I have encountered a problem when trying to pass an object with a specific type in order to update the state. For some reason, the state remains unchanged. My approach involves using the ...

Having trouble performing nested queries with RxJS 6 and the expand operator in Angular, Firebase, and Observables?

I came across a query on Stack Overflow that helped me fetch a random item from a Firebase collection in my Angular 9 app using AngularFire. While the solution works perfectly and gives me the desired output most of the time, there's an issue when th ...

Angular: utilizing a setter method for input manipulation

After diving into the angular docs, a particular sentence caught my attention: REF "evaluation of a template expression should have no visible side effects..." If I correctly grasp the concept, it's because during a "single detection cycle", the " ...

Troubleshooting problem with @Input number in Angular 2

Here is the component in question: <component value="3"></component> This is the code for the component: private _value:number; get value(): number { return this._value; } @Input() set value(value: number) { console.log(v ...

Creating Object of Objects in TypeScript: A Comprehensive Guide

Assuming I have a structure similar to this: interface Student { firstName: string; lastName: string; year: number; id: number; } If I intend to handle an array of these structures, I can simply specify the type as Student[]. Instead of utilizin ...

Uncertain about the distinction between reducers and dispatchers when it comes to handling actions

I'm feeling a bit confused regarding reducers and dispatchers. While both receive actions as parameters, it doesn't necessarily mean that the actions I use in my dispatchers are the same as those used in my reducers, correct? For example, if I h ...

Error: The code encounters a SyntaxError due to an unexpected token '?' in iOS 14

Currently in the process of developing a Headless Shopify project using this starter: https://github.com/vercel/commerce. While testing the demo environment, I encountered some bugs that seem to be specific to iOS 14 or newer. The primary issue stems from ...

Creating multilingual menus with ABP and Nebular

Currently, I am in the process of developing an application using ABP framework version 4.4 and integrating the Nebular theme as opposed to the default basic theme. Amidst various challenges faced during this migration, one particular issue stands out - lo ...

Display the spinner until the data is successfully updated in the DOM using Angular

Dealing with a large dataset displayed in a table (5000 rows), I am attempting to filter the data using column filters. When applying the filter, I have included a spinner to indicate that the filtering operation is in progress. However, due to the quick ...

The specified argument, 'void', cannot be assigned to a parameter that expects 'SetStateAction | undefined'

Currently, I am engaged in a TypeScript project where I am fetching data from an endpoint. The issue arises when I attempt to assign the retrieved data to my state variable nft using the useState hook's setNft function. An error is being thrown specif ...

Is there a way to make the Sweetalert2 alert appear just one time?

Here's my question - can sweetalert2 be set to only appear once per page? So that it remembers if it has already shown the alert. Swal.fire({ title: 'Do you want to save the changes?', showDenyButton: true, showCancelButton: true, ...