Angular select and ng-model

Angular select and ng-model

本文关键字:ng-model and select Angular      更新时间:2023-09-26

我试图用数组中的内容填充一个选择字段。我不知道我的"模式"到底是什么。

我希望访问ctrl.contents的内容,这是一个对象数组。我想这是我的"模型"。

ViewPage.html

<div>
  <select ng-model="ctrl.contents"
    ng-options="content.title.name in content.title.name as content in contents">
  </select>
</div>

var ctrl = this from ViewPage.controller.js

如果我是console.log(ctrl.contents)从ViewPage.controller.js对象数组将返回:

[
  > 0: ContentViewModel
    > title: Object  // Each numbered array object has similar contents 
      name: "Thomas"
      ...
  > 1: ContentViewModel
  > 2: ContentViewModel
  > 3: ContentViewModel
]

我似乎不能得到选择字段填充任何东西。我把ng模型搞错了吗?

您的'model'是一个单独的变量,它将包含您的选择菜单的选定值-将其设置为ctrl.selectedItem

你的ng-options参数应该是这样的:

ng-options="content.title.name for content in ctrl.contents"

要了解更多信息,请参考这个例子-在app.js中同时检出html和js

这应该是你的html

<div>
  <select ng-model="ctrl.selectedContent"
ng-options="content.title.name for content in ctrl.contents" ng-value="content">
  </select>
</div>