bruh bruh - 2 months ago 8
AngularJS Question

Angular select and ng-model

I'm trying to populate a select field with content from an array. I'm confused as to what exactly my "model" is.

I'm looking to access content from

ctrl.contents
which is an array of objects. I assume this is my "model".

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

If I was to
console.log(ctrl.contents)
from ViewPage.controller.js the array of objects would be returned:

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


I can't seem to get the select field populated with anything. Am I getting the ng-model wrong?

Answer

Your 'model' is a separate variable that will contain the selected value of your select menu - set it to something like ctrl.selectedItem.

Your ng-options parameters should look like this:

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

For more info, refer to this example - checkout both the html and js in app.js.

Comments