I need some help. I can't create $scope.headline.category into an object, it has to stay a string. The groupList has to be a list of objects. How can I set the initial value of the dropdown to the 2nd item (1 index)? I can set it through the controller but I have a lot of items and it seems like there should be an easier way... maybe using ng-init? Thank you for any help.
What I tried but doesn't work:
ng-init="headline.category = group.label"
What I tried that does work, but i feel like there's an easier way?
for (var a = 0; a < $scope.headlineList.length; a++) {
for (var b = 0; b < $scope.groupList.length; b++) {
if ($scope.headlineList[a].category == $scope.groupList[b].label) {
$scope.headlineList[a].category = $scope.groupList[b];
}
}
}
Angular
$scope.headline.category = "B";
$scope.groupList = [ {label: "A"}, {label: "B"}, {label: "C"} ];
HTML
<select ng-model="headline.category" ng-options="group.label for group in groupList"></select>
I think you only need the following in the controller:
app.controller('MyCtrl',function($scope){
$scope.headline = { category: "B" }
$scope.groupList = [ {label: "A"}, {label: "B"}, {label: "C"} ];
});
And the select changed to:
<select ng-model="headline.category" ng-options="group.label as group.label for group in groupList"></select>
The key changes being setting headline to the object of { category: "B" } (set this way from some kind of ajax call I'm guessing) and the group.label as at the beginning of the options to indicate the value from the category objects to apply to the model.
Plunk example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With