blue piranha
blue piranha

Reputation: 3874

Dropdown selected value from JSON in AngularJS

Angular newbie question

I have a dropdown showing days from week something like this

<select class="form-control" id="selectedDayofWeek" ng-model="selectedDayofWeek" ng-selected="sdw">
                          <option value="">Select</option>
                          <option value="Monday">Monday</option>
                          <option value="Tuesday">Tuesday</option>
                          <option value="Wednesday">Wednesday</option>
                          <option value="Thursday">Thursday</option>
                          <option value="Friday">Friday</option>
                          <option value="Saturday">Saturday</option>
                          <option value="Sunday">Sunday</option>
                        </select>

In my controller I have

$scope.sdw = json[0].DayofWeek; // Thursday

However this does not selects the dropdown's Thursday value

Upvotes: 1

Views: 754

Answers (1)

Tom Arad
Tom Arad

Reputation: 251

You want the ng-model specified in your HTML to be of the same name on your $scope.

Change $scope.sdw = json[0].DayofMonth into

$scope.selectedDayofWeek = json[0].DayofMonth;

The ng-selected is redundant (and not used well, read about it here).

Upvotes: 2

Related Questions