问题
I am new to Angular.js (and also to this Forum) and want to try out some basics, but I got stucked at working with select and options.
I have a small app and want to add a select-box, but I get a blank option which I want to get rid of.
I was searching here on Stackoverflow and, of course, Google, and tried out may options and hints, but nothing seems to work for me.
I hope someone can help me here...
About the app: I have some Items with attribures like item.Name or item.Price etc. and want to store the value of the selected tag with item.Store.
This is part of the view:
<td>
<select ng-model="item.Store" ng-options="item.storeName for item in items.Stores">
</select></td>
And this is what I have in my controller:
$scope.items.Stores = [
{storeId : 1, storeName : 'Spar' },
{storeId : 2, storeName : 'Billa' },
{storeId : 3, storeName : 'Merkur' },
];
Amongst others I tried this one:
$scope.item.Store = $scope.items.Stores[1];
But I always get the error message: "$scope.item is undefined...." although my ng-model is also named item.Store, so I assume there is a problem with the . or something like that...
Thanks for your help and if something relevant from the code is missing, please tell me :-)
回答1:
Have a look to this Plunker which contains the modifications mentionned in the comments of the OP.
$scope.items = {
stores: [
{id : 1, name : 'Spar' },
{id : 2, name : 'Billa' },
{id : 3, name : 'Merkur' },
]
};
$scope.item = {
store: $scope.items.stores[1]
}
来源:https://stackoverflow.com/questions/16114096/angularjs-alway-blank-element-in-select-tag