Autocomplete Component - Cannot clear Template-driven value · Issue #249 · coreui/coreui-angular · GitHub
Skip to content

Autocomplete Component - Cannot clear Template-driven value #249

Open

Description

@bernik1980

I have a simple autocomlete input:

 <input id="..."
        [(ngModel)]="value"
        cAutocomplete
        ...

The value is a simple signal:
value = signal('')

After doing an external search following (inputChange) and reacting on (optionChange) with another http request, I want to clear the current displayed value of the input:

value.set('')

But the value of the last selected option remains displayed.

A cleaner does work, but I want to clear the value programmatically.

Activity

  1. self-assigned this
    on Apr 13, 2026
  2. xidedix commented on May 11, 2026

    @xidedix
    Member

    @bernik1980
    fixed in ~5.6.23

  3. bernik1980 commented on May 26, 2026

    @bernik1980
    Author

    @xidedix
    I was not able to test the fix, because another bug (#235) was re-introduced with 5.6.23, which did force me to go back to 5.6.22:

    When using the autocomplete component with reactive forms and assigning a value to the related form group control, the (optionChange) callback will be called without a value. This was then, and is now, a show-stopper. Could you please look into this again?

  4. xidedix commented on May 28, 2026

    @xidedix
    Member

    @bernik1980
    We're having trouble confirming your (optionChange) callback issue.
    Could you please take a look at this example? (see console log)
    https://coreui.io/angular/docs/forms/autocomplete/#implementation-example

  5. bernik1980 commented on May 28, 2026

    @bernik1980
    Author

    @xidedix
    I took a look at your new example. And on further testing, the mentioned bug only happens, if the formControl is empty before assigning a value. This should really be the default case. since you wont assign a static value to the control in a production environment.

    Works fine:

    protected formGroup = new FormGroup({
        userName: new FormControl<string>('Barbara')
      });
    
    this.formGroup.get('userName')?.setValue('Markus');
    

    Unwanted (optionChange) callback occurs:

    protected formGroup = new FormGroup({
        userName: new FormControl<string>('')
      });
    
    this.formGroup.get('userName')?.setValue('Markus');
    

    Note: I did not test the specified example, which I took from your documentation. But I confirmed this in my own environment, which works nearly the same. So I hope, you can reproduce this issue.

  6. bernik1980 commented on May 28, 2026

    @bernik1980
    Author

    I can confirm the value.set('') fix though. Thank you for that!

    I will close this issue, as soon as we worked out the (optionChange).

  7. xidedix commented on May 28, 2026

    @xidedix
    Member

    @bernik1980

    protected formGroup = new FormGroup({
      userName: new FormControl<string>(''),
    });

    where (optionChange) does not fire

  8. bernik1980 commented on May 29, 2026

    @bernik1980
    Author

    @xidedix
    I tried my best to reproduce this issue in your example, but was not able to achieve that.

    What I can say is, that this does not happen with 5.6.22 but with 5.6.24. (Most likely with 5.6.23 too, but i only testet 5.6.24 recently.) So the fix for the main topic of this issue seems responsible.

    Another finding ist, that (optionChange) is only called after [delay] has been passed after assigning a value to the FormControl. So if I increase that value to 2500, I can see my assigned value for 2500ms until (optionChanged) is called without a value.

    Additionally I have to say, that my project uses some newer library then stackblitz and that it is zoneless. But thats just poking in the dark.

    Do you have any idea left, what could be the reason?

  9. xidedix commented on May 29, 2026

    @xidedix
    Member

    @bernik1980 we'll keep digging

  10. bernik1980 commented on Jun 12, 2026

    @bernik1980
    Author

    I digged a little bit myself and can now reproduce the issue. Since I have no account on Stackblitz, I will describe my changes here.

    The issue will happen in combination with a http request started in ngOnInit. Starting the http request on a button click will not yield the error, it has to be in the ngOnInit function.

    So here is how to extend your example:
    (If you need the imports too, let me know.)

    • Extend the constructor:
      constructor(private http: HttpClient) {

    • Let the class implement OnInit:
      export class Autocomplete16Component implements OnInit {

    • Implement ngOnInit:
      ngOnInit() { this.http.get('https://apitest.coreui.io/demos/users').subscribe({ next: () => { this.formGroup.get('userName')?.setValue('Markus') } }) }

    Wait for the container to reload. You now will see 'Markus' displayed for 1500ms (delay of the Autocomplete) and then its cleared.

    If there is something wrong with my basic approch, I would really like to know. I start API requests in ngOnInit very often, to load data for the component.

    @xidedix And just to be sure: This does not happen, if the FormControl has an inial value other then ''

    Thank you for all the time you investing in this!

  11. xidedix commented on Jun 14, 2026

    @xidedix
    Member

    Thanks for the details. We were able to reproduce your issue.

  12. xidedix commented on Jun 15, 2026

    @xidedix
    Member

    @bernik1980 we’ve identified the problem and are working on the patch.

  13. bernik1980 commented on Aug 16, 2026

    @bernik1980
    Author

    Any news on this? I had to implement a workarround using "loading" as initial formcontrol value, to avoid the reset.

  14. xidedix commented on Oct 3, 2026

    @xidedix
    Member

    fixed in ~5.7.34 for Angular 22

    Upgrade note:
    Selecting an option now writes the option’s value to the control (e.g., person.id), not its label. The valueChange event emits that value. Setting a label from code still displays it.

  15. deleted a comment from github-actions on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions