【问题标题】:Angularjs alway blank element in select tagAngularjs在选择标签中总是空白元素
【发布时间】:2013-04-13 09:44:57
【问题描述】:

我是 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 未定义....” 虽然我的 ng-model 也被命名为 item.Store,所以我认为 .或类似的东西......

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

【问题讨论】:

  • 你应该试试$scope.item = { Store: ... } item 还不是一个对象,你必须先创建它然后设置 store 属性。
  • 好的,我在想对象会在作用域自动创建。无论如何,我也认为 ng-options 有问题,从逻辑上讲它应该是 'ng-options="item.Store.storeName for item.Store in item.Stores"' 但那也不起作用......跨度>
  • 我认为您的列表也是如此:$scope.items = { stores: [ ... ] };
  • 感谢这个作品也很完美!

标签: javascript select view angularjs controller


【解决方案1】:

查看此Plunker,其中包含 OP 的 cmets 中提到的修改。

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

$scope.item = {
    store: $scope.items.stores[1]
}

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 2013-03-07
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2021-03-26
    • 2015-09-08
    • 2014-04-30
    • 2016-12-11
    相关资源
    最近更新 更多