1

I am trying to set selected value in drop down by passing value from back-end. I am able to set selected attribute true for option I want, but it does not reflect on UI. When I view elements of page in browser I can see it correctly being set but can see any label in UI.

<option value="2: Object" ng-reflect-ng-value="[object Object]" ng-reflect-selected="true">

template:

<select  class="form-control" id = {{id}} [(ngModel)]="bindModelData" (ngModelChange)="onSelectData($event)" [disabled]="disabled">
  <option *ngFor="let object of objects"  [ngValue]="object"
          selected="{{object.description=== bindModelData?.description ?true:null}}>
    {{object.description}}
  </option>
</select>

UI Element after it is set:

<select class="form-control ng-untouched ng-pristine ng-valid" ng-reflect-model="[object Object]" ng-reflect-id="category" id="category">
  <!--template bindings={
  "ng-reflect-ng-for-of": "[object Object],[object Object],[object Object],[object Object],[object Object]"
}--><option value="0: Object" ng-reflect-ng-value="[object Object]">
    AAA
  </option><option value="1: Object" ng-reflect-ng-value="[object Object]" ng-reflect-selected="true">
    BBB
  </option><option value="2: Object" ng-reflect-ng-value="[object Object]">
    CCC
  </option><option value="3: Object" ng-reflect-ng-value="[object Object]">
    DDD
  </option><option value="4: Object" ng-reflect-ng-value="[object Object]">
    EEE
  </option>
</select>
  • did you tried : [selected]="object.description=== bindModelData?.description ?true:null" – pritesh agrawal Jun 16 '17 at 06:54
  • Can you show me the value of `objects`? – Mr_Perfect Jun 16 '17 at 06:56
  • Getting data from backend means? Provide plunker if possible. – Sai M. Jun 16 '17 at 06:57
  • Yes, no change with that – naveen singh Jun 16 '17 at 07:01
  • What I am trying to achieve is data retention in drop down when I navigate between views.When I navigate away I store in localstorage and when I come back I use it to set selected. I am able to set value to property and selected property is set to true but same is not reflected in UI. When i come back this is the element i see – naveen singh Jun 16 '17 at 07:13
  • If you are storing values in localStorage then why not write a method which will get the value from the localStorage with `localStorage.getItem('key')` and set the selected property accordingly. – Abrar Jun 16 '17 at 07:42
  • @Abrar I am doing that and it is getting binded with the variable, only problem is when I see UI it is still blank, nothing is selected in UI. If you see my edited post in section " UI Element after it is set" element is set properly but it doesn't reflect on UI. – naveen singh Jun 16 '17 at 07:51
  • 1
    @naveensingh, what i have understood is that you want the options that you previously `selected` to hold the `selected` property, right ? – Abrar Jun 16 '17 at 07:58
  • @Abrar that is correct. I want to retain changes made on page when i navigate from one page to other – naveen singh Jun 16 '17 at 08:02

0 Answers0