I have an array of objects:
$scope.allCategories = [{'name': 'cat1', 'description: ... },
{'name': 'cat2', 'description: ... },
{...}];
I was previously using:
<select ng-model="mycategory" ng-change="test()">
<option ng-repeat="category in allCategories">{{ category.name }}</option>
</select>
Which worked fine, but it's not the "Angular way". So I have changed it to:
<select ng-model="mycategory" ng-change="test()" ng-options="category.name for category in allCategories track by category.name"></select>
Which builds this correct html:
<select ...>
<option value="cat1" label="cat1">cat1</option>
<option value="cat2" label="cat2">cat2</option>
...
</select>
However in my test()
:
// called whenever select changes
$scope.test = function() {
console.log($scope.mycategory);
}
$scope.mycategory
prints out the object {'name': 'cat1', 'description: ... }
instead of the string value. The previous version with ng-repeat
would give me the string value, so this threw me off.
Is there a way to get the string value automatically without doing $scope.mycategory = $scope.mycategory['name'];
inside my test()
?