javascript - Input value not changing in Angular - Stack Overflow

I have a custom ponent where the user can search for values with a dropdowninput bination and click th

I have a custom ponent where the user can search for values with a dropdown/input bination and click the result or scroll and search in the dropdown and click the value, this will set the input value to the clicked dropdown value.

Assume the values are: ['foo', 'bar', 'fee']

If I click 'foo' in the dropdown, the input value will as expected change to 'foo'. However, if then I backspace a bit to 'f' and then click 'foo' again, it will set the variable to 'foo', but it won't update the dropdown value.

If my value was 'foo', I backspaced and I clicked 'fee', it does update like it should.

What is the problem of my value not updating in the input?

search-select.html:

<div>
    <input type="text"
        (input)="filterDropdown()"
        [(ngModel)]="filterValue" [value]="selectedItemName"/> <-- no change here
        {{selectedItemName}} <-- does change here (but I don't want it here)
    <div>
        <div *ngFor="let item of filteredItems" (click)="setItem(item)">
            {{item.name}}
        </div>
</div>

search-select.ts:

public setItem(item): void {
        this.selectedItemName = item[this.itemName];
}

I have a custom ponent where the user can search for values with a dropdown/input bination and click the result or scroll and search in the dropdown and click the value, this will set the input value to the clicked dropdown value.

Assume the values are: ['foo', 'bar', 'fee']

If I click 'foo' in the dropdown, the input value will as expected change to 'foo'. However, if then I backspace a bit to 'f' and then click 'foo' again, it will set the variable to 'foo', but it won't update the dropdown value.

If my value was 'foo', I backspaced and I clicked 'fee', it does update like it should.

What is the problem of my value not updating in the input?

search-select.html:

<div>
    <input type="text"
        (input)="filterDropdown()"
        [(ngModel)]="filterValue" [value]="selectedItemName"/> <-- no change here
        {{selectedItemName}} <-- does change here (but I don't want it here)
    <div>
        <div *ngFor="let item of filteredItems" (click)="setItem(item)">
            {{item.name}}
        </div>
</div>

search-select.ts:

public setItem(item): void {
        this.selectedItemName = item[this.itemName];
}
Share Improve this question edited Apr 26, 2019 at 9:43 Jur asked Apr 26, 2019 at 9:21 JurJur 1852 gold badges3 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Try this this works on native html select tag. HTML:

Use (change)="selectValue($event)" on <select> 
Use directive [(ngModel)]="selectedValue" on <select> 

TS:

selectedValue: any;
selectValue(ev){
   this.selectedValue = ev.target.value;
}

Found the solution

(input)="filterDropdown($event.target.value)"

and in the ts file:

this.selectedItemName = value;

Try to use (change)=.. instead of (input)=..

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743725756a4496587.html

相关推荐

  • javascript - Input value not changing in Angular - Stack Overflow

    I have a custom ponent where the user can search for values with a dropdowninput bination and click th

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信