1
votes

I'm trying to programmatically update the selected item from my controller and it's not working. When I click the submit button, all it does is clear out the select. What I expect is that the second option (Perth) gets selected.

Look at this plunker for more info. http://jsfiddle.net/ky5F4/

Thanks

<div ng-controller="MyController" ng-app>
    <div>Number of datasets= {{datasets.length}}</div>
    <div>
        <select class="dataset" size="1" ng-model="selectedDataset">
            <option ng:repeat="dataset in datasets" value="{{dataset.name}}">
                <h3>{{dataset.name}}</h3>
            </option>
    </div>
    <input type="button" value="submit" ng-click="Select()"></input>
</div>

function MyController($scope) {
    $scope.datasets = [{
        id: 'id-1',
        name: 'Brisbane'
    }, {
        id: 'id-2',
        name: 'Perth'
    }, {
        id: 'id-3',
        name: 'Melbourne'
    }];

    $scope.selectedDataset = 'id-1';

    $scope.Select = function () {
        alert('testing');
        $scope.selectedDataset = 'id-2';
    }
}
1
I see you have ng:repeat instead of ng-repeat...not that this will help the issue - tjoenz
yeah I realized, copied someone else's fiddle. - fbhdev

1 Answers

12
votes

Use ng-options instead of ng-repeat.

<select class="dataset" size="1" ng-model="selectedDataset" 
    ng-options="dataset.id as dataset.name for dataset in datasets">

Working fiddle

You can see the docs here