question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to bind MD input defaut value to autocomplete

See original GitHub issue

Bug, feature request,

Unable to bind MD input defaut value to autocomplete

What is the expected behavior?

We have a service which returns the result for the inputs. Now iam using mdinput with autocomplete. I can see the results of the autocomplete popped up but not the default value which is assigned to md-input. Below is my code.

Below is the code ::

<md-input-container >
      <input mdInput [mdAutocomplete]="primary" [(ngModel)]="primaryRoleDetails"  (ngModelChange)="filterPrimaryRoles = filterRoles($event)"  [formControl]="primaryRoleCtrl">
 </md-input-container>
 <md-autocomplete #primary="mdAutocomplete"  class="addautoComplete" [displayWith]="displayRoles">
                              <md-option *ngFor="let roles of filterPrimaryRoles"  [value]="roles" >
                                   {{roles?.jobrolecode}}
                              </md-option>
                           </md-autocomplete>

Here primaryRoleDetails is the default value , filterPrimaryRoles is the list of values in the autocomplete.

Any idea on this please?

Iam using Angular 4 + Material Beta 3 version.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
omprakash-sharmacommented, Oct 16, 2018

Can be set or reset formControl as…

 ngOnInit() {
       this.primaryRoleCtrl = new FormControl({value: 'defaultValue'});
      // or can be reset FormControl with default value.
      this.primaryRoleCtrl.reset('defaultValue');
  }
4reactions
timmyrosencommented, Sep 13, 2017

I think I had the same problem as you.

Here is my solution:

<md-form-field class="Autocomplete" [floatPlaceholder]="floatPlaceholder">
    <input type="text" #textInput class="Autocomplete__input" [placeholder]="placeholder" aria-label="Choose an option" mdInput [formControl]="autocompleteControl" [mdAutocomplete]="auto">
    <md-progress-bar class="Autocomplete__progressbar" [mode]="'indeterminate'" [hidden]="!IS_LOADING"></md-progress-bar>
    <md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn" (optionSelected)="optionSelected($event)">
        <md-option *ngFor="let option of options | async" [value]="option">
            {{ option.autocompleteLabel }}
        </md-option>
    </md-autocomplete>
</md-form-field>

I create a template variable on the input field, and then in my controller:

@ViewChild('textInput') textInput: ElementRef;

ngOnInit() {
    this.textInput.nativeElement.value = 'Your value here';
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to bind MD input defaut value to autocomplete #4451
Now iam using mdinput with autocomplete. I can see the results of the autocomplete popped up but not the default value which is...
Read more >
To make default selection in autocomplete (material)
Use FormControl SetValue method to set Default Value this.myControl.setValue( {name: 'Mary'});.
Read more >
Directives > mdAutocomplete - AngularJS Material
Parameter Type Description md‑search‑text expression expression A model to bind the search query text to. md‑selected‑item object object A model to bind the selected item...
Read more >
Autocomplete displayValue and manually set value
Hi,. I'm using the mdbautocomplete with a server side search. When user selects something, i want to keep the UserSelected object, and write...
Read more >
Form Input | Components - BootstrapVue
By default, range inputs "snap" to integer values. To change this, you can specify a step value. In the example below, we double...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found