Angularjs alway blank element in select tag

耗尽温柔 提交于 2019-12-11 12:02:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!