components icon indicating copy to clipboard operation
components copied to clipboard

Unable to bind MD input defaut value to autocomplete

Open vibingopal opened this issue 9 years ago • 13 comments

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.

vibingopal avatar May 09 '17 23:05 vibingopal

Any inputs on this please?

vibingopal avatar May 10 '17 16:05 vibingopal

Can we have a plunker please.

shaunak1111 avatar May 11 '17 15:05 shaunak1111

Ok. Here is the plunker. The default value for Input is not getting populated.

https://plnkr.co/edit/rcEabQzoLVr9SNLeelGP?p=preview

vibingopal avatar May 11 '17 18:05 vibingopal

Set the default value in new FormControl().

https://plnkr.co/edit/nC4YlacA3RcZ5cHuWqWf?p=preview

I don't believe this is a bug with material, rather your mixing of template-driven/reactive forms.

Note: there was also a bug in how you were filtering the items, you can see the fix in the plunk

willshowell avatar May 11 '17 19:05 willshowell

Thank you . But as per your example, how you are assigning the value of service into the input. In the Form Control, you just referred the variable.Not the variable value.

vibingopal avatar May 11 '17 20:05 vibingopal

I don't think I follow. You could just as easily do this:

this.primaryRoleCtrl = new FormControl({
  roleCode:"4 4 TEMP",
  roleName:"ABC",
  position :"CDE",
  result:"asdas",
  result1:"asdasdas"
});

willshowell avatar May 11 '17 20:05 willshowell

Sorry Will if i confused you. When i mapped the value directly in the form control it is not working.

this.primaryRoleCtrl = new FormControl(this.resultFromService.roleCode); https://plnkr.co/edit/WSrZfsZRs6B7hX4vpQxe?p=preview

My question is how you are binding the value of the service (resultFromService) to Input element.You just referred the variable name in the input field not the attribute binded to that.

 <md-input-container>
   <input mdInput [mdAutocomplete]="primary"  (ngModelChange)="filterPrimaryRoles = filterRoles($event)" [formControl]="primaryRoleCtrl"> 
</md-input-container>

what if the attribute name returned from service(roleCode) and the one binded in autocomplete ({{roles?.roleCode}}) is different.

vibingopal avatar May 11 '17 20:05 vibingopal

Ok if I understand correctly, you want to use the roleCode given by your service to select the default value.

https://plnkr.co/edit/dYNEz53s7cvWhI26r1LN?p=preview

If, at a later time, you get new data from your service and want to update the default value, you can do primaryRoleCtrl.setValue().

Still though, unless I'm missing something, this doesn't seem to be any bug with material itself.

willshowell avatar May 11 '17 20:05 willshowell

I am not saying there is a bug with material . My question is what if the service variable returned(roleCode1) and the autocomplete attribute(roleCode) mapped is different.

https://plnkr.co/edit/iCkHeQBXMZbemYiVTW5c?p=preview

what if we need to assign a default value to Mdinput and the default value may or may not in autocomplete list box.

vibingopal avatar May 11 '17 21:05 vibingopal

This doesn't look like a bug in Material. Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

kara avatar May 11 '17 23:05 kara

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';
}

timmyrosen avatar Sep 13 '17 11:09 timmyrosen

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');
  }

omprakash-sharma avatar Oct 16 '18 06:10 omprakash-sharma

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.