I am working on a POC in which i am facing a challenge. Please help me solving it.
Situation
- I have list of categories which needs to be displayed in a select dropdown
- I have a portal object which contains one of the categories in the select dropdown
Challenge: I need to select the option which matches the category of the portal while populating the select dropdown
Category select dropdown population and prtl-category binding is,
<!-- Portal Category -->
<div class="row prtl_field_margin">
<div class="col-4 prtl_form_field_right">
<label for="pCategory" class="control-label prtl_form_label">Category</label>
</div>
<div class="col-4">
<select class="form-control" id="pCategory" name="pCategory" [(ngModel)]="prtl.category">
<option value="" disabled>Choose a Category</option>
<option *ngFor="let c of categories" [ngValue]="c">{{ c.name }}</option>
</select>
</div>
<div class="col-4"></div>
</div>
prtl object data in component ts file is,
{
"name": "Stock Market",
"id": 10,
"category": {
"id": "1",
"name": "Cricket"
}
}
Category array data in component ts file is,
[
{
"id": "1",
"name": "Cricket"
},
{
"id": "2",
"name": "Football"
}
]
Result needed: Categories dropdown should contain all the categories in the array. However, the category which matches to the category of prtl object should be selected. As in, the cricket category should be selected after dropdown is populated, but the user is free to change it to football.