1

我是 Angular.js 的新手(也是这个论坛的新手),想尝试一些基础知识,但我一直在使用 select 和 options。

我有一个小应用程序并想添加一个选择框,但我得到了一个我想摆脱的空白选项。

我在 Stackoverflow 上搜索,当然还有 Google,并尝试了可能的选项和提示,但似乎没有什么对我有用。

我希望有人可以在这里帮助我...

关于应用程序:我有一些具有 item.Name 或 item.Price 等属性的项目,并希望将所选标签的值存储在 item.Store 中。

这是视图的一部分:

<td>
<select ng-model="item.Store" ng-options="item.storeName for item in items.Stores">
</select></td>

这就是我的控制器中的内容:

$scope.items.Stores = [
    {storeId : 1, storeName : 'Spar' },      
    {storeId : 2, storeName : 'Billa' },
    {storeId : 3, storeName : 'Merkur' }, 
];

其中,我尝试了这个:

$scope.item.Store = $scope.items.Stores[1];

但我总是收到错误消息:“$scope.item is undefined....”虽然我的 ng-model 也被命名为 item.Store,所以我认为 . 或类似的东西...

感谢您的帮助,如果缺少与代码相关的内容,请告诉我:-)

4

1 回答 1

0

看看这个Plunker,其中包含 OP 评论中提到的修改。

$scope.items = {
    stores: [
      {id : 1, name : 'Spar' },      
      {id : 2, name : 'Billa' },
      {id : 3, name : 'Merkur' }, 
    ]
};

$scope.item = {
    store: $scope.items.stores[1]
}
于 2013-04-19T22:58:29.480 回答