簡體   English   中英

角度選擇和ng模型

[英]Angular select and ng-model

我正在嘗試使用數組中的內容填充選擇字段。 我對我的“模型”到底是什么感到困惑。

我想從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來自ViewPage.controller.js

如果我要從ViewPage.controller.js到console.log(ctrl.contents) ,則將返回對象數組:

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

我似乎無法在select字段中填充任何內容。 我弄錯了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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM