Unable to bind MD input defaut value to autocomplete
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.
Any inputs on this please?
Can we have a plunker please.
Ok. Here is the plunker. The default value for Input is not getting populated.
https://plnkr.co/edit/rcEabQzoLVr9SNLeelGP?p=preview
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
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.
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"
});
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.
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.
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.
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.
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';
}
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');
}
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.