【问题标题】:Angular JS - Bind drop down (ng-options) to custom object and show selected valueAngularjs - 将下拉菜单(ng-options)绑定到自定义对象并显示所选值
【发布时间】:2015-10-05 18:45:35
【问题描述】:

我想要的是在打开编辑书表单时在 ddl 上显示初始值。但它没有显示任何值,请参阅我用来绑定 ddl 的代码——新书和作者是上面定义的 JSON 数据。

下拉是绑定到作者 json 数组,格式为这种格式 -

{id:1, Name: XYZ}

这个 ddl 是具有以下格式的书籍的编辑表单的一部分 -

newbook = {
    Id:bctx.bookDetail.Id,
    Author: { Id: bctx.bookDetail.AuthorId, Name: bctx.bookDetail.AuthorName },
    Genre: bctx.bookDetail.Genre,
    Price: bctx.bookDetail.Price,
    Title: bctx.bookDetail.Title,
    Year: bctx.bookDetail.Year
};   


<select ng-options="author as author.Name for author in actrl.authors" ng-model="bctrl.newbook.Author" required>
    <option value="">--Select--</option>
</select>  

【问题讨论】:

  • 修复了,使用 ng-options="author as author.Name for author in actrl.authors track by author.Id"

标签: angularjs angularjs-ng-options


【解决方案1】:

我认为选择选项只能是简单的对象,因此您可能希望将选择更改为 id(或任何其他数字/字符串值)。您可以通过监听 change 事件在控制器中检索实际选择的作者对象。

在查看the documentation 之后,您可能会查看“track by”以启用复杂的对象选择。

【讨论】:

  • 您的回答有帮助..我能够解决问题..希望将其标记为答案,但没有足够的声誉..谢谢..
  • 我认为您不需要声誉来将其标记为答案。你只需要稍等片刻。它应该告诉你多长时间。 @narendrabagdwal
  • 谢谢@narendrabagdwal。你能告诉我们具体的修复方法是什么吗?
  • 修复了,使用 ng-options="author as author.Name for author in actrl.authors track by author.Id"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多