Tried to change dropdown value from child component in angular 8 but i do not know how to do it.If anyone know please help to find solution.
If i click France button want to set dropdown value France. If i click Germany button want to set dropdown value Germany. If i click Italy button want to set dropdown value Italy.
app.component:
<select name="city" class="rounded-inputs20 select-select col-md-3" (change)="onChange($event.target.value)">
<option *ngFor="let country of countries" [value]="country.id">{{country.name}}</option>
</select>
body.component:
<button (click)="changeVal('France')">France</button>
<button (click)="changeVal('Germany')">Germany</button>
<button (click)="changeVal('Italy')">Italy</button>
Demo::https://stackblitz.com/edit/angular-dropdown-geishz?file=app%2Fapp.component.html
You need to implement event binding here to pass the data from
bodycomponent toappcomponent and for which you need to make use of Output() and EventEmitter() .So modified files:
body.component.ts:
Include
[(ngModel)]='selectedCountry'to track the dropdown changes in app component html as follows,app.component.html:
In above code,
(valueChange)="getValueChange($event)"is where you receive the data from the respective button clicked.And then in app component ts file, you can include a function to handle the button click changes happen in body component and change the dropdown selection by setting the value of
this.selectedCountry = selectedOption.id,app.component.ts:
Forked Stackblitz: