Within the select element, I have an option set up like this:
<option [selected]=" impulse === 10 || isTraining " value="10">10</option>
My assumption was that when any value is assigned to impulse
and isTraining
is set to true
, the current option would be automatically selected due to the presence of the selected keyword in the dropdown. However, this does not seem to be happening. Are logical operators ever evaluated for attributes requiring a boolean value?
The complete select code snippet is as follows:
<select class="form-control" name="flashFormatImpulse" id="field_flashFormatImpulses"
(change)="handleFlashImpulseSelection($event)" [(ngModel)]="impulseSelection" [disabled]="isStarted">
<option *ngIf="!isTraining" [selected]="impulse === 1" value="1">1</option>
<option *ngIf="!isTraining" [selected]="impulse === 2" value="2">2</option>
<option *ngIf="!isTraining" [selected]="impulse === 3" value="3">3</option>
<option *ngIf="!isTraining" [selected]="impulse === 4" value="4">4</option>
<option *ngIf="!isTraining" [selected]="impulse === 5" value="5">5</option>
<option *ngIf="!isTraining" [selected]="impulse === 6" value="6">6</option>
<option *ngIf="!isTraining" [selected]="impulse === 7" value="7">7</option>
<option *ngIf="!isTraining" [selected]="impulse === 8" value="8">8</option>
<option *ngIf="!isTraining" [selected]="impulse === 9" value="9">9</option>
<option [selected]="impulse === 10 || isTraining" value="10">10</option>
</select>
Update
https://i.sstatic.net/fFrjL.png
This is how the code appears after minor modifications:
<div class="form-group">
<label class="form-control-label" jhiTranslate="businesslogic.flashs.flashcard.impulse"
for="field_flashFormatImpulses">Flash Impulse</label>
<select class="form-control" name="flashFormatImpulse" id="field_flashFormatImpulses"
(change)="handleFlashImpulseSelection($event)" [(ngModel)]="impulseSelection" [disabled]="isStarted">
<option *ngIf="!isTraining" [selected]="impulse === 1" value="1">1</option>
<option *ngIf="!isTraining" [selected]="impulse === 2" value="2">2</option>
<option *ngIf="!isTraining" [selected]="impulse === 3" value="3">3</option>
<option *ngIf="!isTraining" [selected]="impulse === 4" value="4">4</option>
<option *ngIf="!isTraining" [selected]="impulse === 5" value="5">5</option>
<option *ngIf="!isTraining" [selected]="impulse === 6" value="6">6</option>
<option *ngIf="!isTraining" [selected]="impulse === 7" value="7">7</option>
<option *ngIf="!isTraining" [selected]="impulse === 8" value="8">8</option>
<option *ngIf="!isTraining" [selected]="impulse === 9" value="9">9</option>
<option [selected]="true" value="10">10</option>
</select>
</div>
<div>
<select class="form-control">
<option [selected]="true" value="10">10</option>
</select>
</div>
In both cases, [selected]
is used with different values but produces the same outcome.
Update 2
https://i.sstatic.net/22zhD.png
Here is the revised version of the code:
<div class="form-group">
<label class="form-control-label" jhiTranslate="businesslogic.flashs.flashcard.impulse"
for="field_flashFormatImpulses">Flash Impulse</label>
<select class="form-control" name="flashFormatImpulse" [(ngModel)]="impulseSelection" id="field_flashFormatImpulses"
(change)="handleFlashImpulseSelection($event)" [disabled]="isStarted">
<option *ngIf="!isTraining" [selected]="impulse === 1" value="1">1</option>
<option *ngIf="!isTraining" [selected]="impulse === 2" value="2">2</option>
<option *ngIf="!isTraining" [selected]="impulse === 3" value="3">3</option>
<option *ngIf="!isTraining" [selected]="impulse === 4" value="4">4</option>
<option *ngIf="!isTraining" [selected]="impulse === 5" value="5">5</option>
<option *ngIf="!isTraining" [selected]="impulse === 6" value="6">6</option>
<option *ngIf="!isTraining" [selected]="impulse === 7" value="7">7</option>
<option *ngIf="!isTraining" [selected]="impulse === 8" value="8">8</option>
<option *ngIf="!isTraining" [selected]="impulse === 9" value="9">9</option>
<option [selected]="impulse === 10 || isTraining" value="10">10</option>
</select>
</div>
<div class="form-group">
<select class="form-control" >
<option *ngIf="!isTraining" [selected]="impulse === 3" value="3">9</option>
<option *ngIf="!isTraining" [selected]="impulse === 9" value="9">9</option>
<option [selected]="impulse === 10 || isTraining" value="10">10</option>
</select>
</div>
<div>impulse {{impulse}}, isTraining {{isTraining}}, result: {{impulse === 10 || isTraining}}</div>
<select class="form-control">
<option [selected]="impulse === 10 || isTraining" value="10">10</option>
</select>
The issue with selection seems to stem from using [(ngModel)]="impulseSelection"
for the selector along with its initialization as
impulseSelection = 3;
Under these circumstances, the binding linked to impulseSelection
continually overrides the selector's value to 3, even though it should not. It appears that the only viable solution is to enforce a value of 10 on impulseSelection
when isTraining
is set to true.