1

The select element is not bound as expected.

html:

<select ng-model="SelectedPage" 
 ng-change="ShowPageResult();" ng-options="o for o in PageNumbers">
</select>

ctrl.js:

function BindPageNumbers() {        
        $scope.PageNumbers = [];
        for (var i = 1; i <= 2) ; i++) {
            $scope.PageNumbers.push(i);
        }      
    }

output:

<option value="0"label="1">1</option>
<option value="1" label="2">2</option>

if i put $scope.PageNumbers.push(i.toString());, then the output is

<option value="?"label=""></option>
<option value="0" label="1">1</option>
<option value="1" label="2">2</option>

expected:

<option value="1">1</option>
<option value="2">2</option>

what should be done to get the desired o/p : http://jsfiddle.net/s222904f/

KreepN
  • 8,528
  • 1
  • 40
  • 58
sukesh
  • 2,379
  • 12
  • 56
  • 111
  • 2
    Arrays are zero based so the output is exactly as expected. If you want to have a value other than what you get, you can push an object with a label and a value and use the "select as" part of the ngOptions expression. – jme11 Jul 08 '15 at 13:39

2 Answers2

2

In your controller:

$scope.PageNumbers = [];
for (var i = 1; i <= 2 ; i++) {
    $scope.PageNumbers.push({ id: i, value: i });
}

In your view:

<select ng-model="SelectedPage"    
    ng-options="o.id for o in PageNumbers track by o.value"></select>
von v.
  • 16,868
  • 4
  • 60
  • 84
  • @Qwerty That's cause he changed your binding structure in his answer and you can't use that line when binding to an object. – KreepN Jul 08 '15 at 14:05
0

Check it: http://jsfiddle.net/khwuv4Lj/

Per the answer here, the empty option exists because the

ng-model="SelectedPage"

line binds it to an empty string. You can avoid this by setting the default value if the select in the scope.

Edit: Plnkr was updated to fix the OP's comment.

enter image description here

Community
  • 1
  • 1
KreepN
  • 8,528
  • 1
  • 40
  • 58