yaru
yaru

Reputation: 1310

Why <option selected="selected"> does not work?

My ng-selected expression sets selected="selected" html attribute in <option> tag ( you can see it in screenshot http://prntscr.com/bmgozg ) but for some unknown to me reason this <option> element is still not selected on web page...

I use Angular 1.4.7.

<select ng-model="link.destination_slide_number">
                        <option value="{{$index + 1}}"
                                ng-repeat="canvas in vm.canvases"
                                ng-selected="link.destination_slide_number == $index + 1">Go to slide #{{$index + 1}}
                        </option>
</select>

Upvotes: 1

Views: 420

Answers (2)

yaru
yaru

Reputation: 1310

Thanks Per Hornshøj-Schierbeck, I was able to achieve a success with this code:

html

<select ng-model="link.destination_slide_number"
        ng-options="idx * 1 + 1 as vm.formatText(idx) for (idx, choice) in vm.canvases">                        
</select>

js

function formatText(canvasIdx) {            
            return 'Go to slide #' + (Number(canvasIdx) + 1);
        }

Upvotes: 0

You can (will) run into some strange issues if you repeat over option elements in a select. Angular implemented a directive ng-options to help you with that: https://docs.angularjs.org/api/ng/directive/ngOptions

Upvotes: 1

Related Questions