Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to deal with nested data in Angular.js select in a form?

How to deal with nested data in Angular.js select in a form?

Problem

I have a node.js app where the form has a selector. The app loads the data from mongodb via mongoose and populates a nested referenced object. So my data looks like:

{ "__v" : 0,
  "_id" : ObjectId( "51199c96c13e06ae33000001" ),
  "consortium" : 
     { _id: '510f234198e6c75a3a1b43eb',
       imageURLsm: '/asdf.jpg',
       name: 'COOLNAME' },
  "description" : "My Description",
  "gdp" : 1234,
  "imageURL" : "http://foo.com",
  "location" : "http://bar.com",
  "name" : "My Cool Name",
  "ownerKey" : "d15e6de2-66c0-4e0c-a0bc-2023b65e87d0",
  "ownerName" : "Asdf Jkls",
  "parcelId" : "eb7fxbc4-581f-83c7-6abe-6e1e0474520f",
  "people" : 1 }

Then in my jade template I have:

div.title.name
    label(for='name') Name:
    input(ng-model='form.name', name='name')
div.title.description
    label(for='description') Description:
    textarea(ng-model='form.description', name='description', cols='50', rows='15')
div.title.consortium.name
    label(for='consortium.name') Consortium:
    select(ng-model='form.consortium.name', name='consortium.name', ng-options="consortium.name for consortium in consortia")
div.title.gdp
    label(for='gdp') GDP:
    input(ng-model='form.gdp', name='gdp')
    p
        button(ng-click='savePlanet()') Save

When I click the save button, it saves everything except the Consortium. The list of various consortiums properly populates because elsewhere, I have set $scope.consortia to equal all the various ones. And whenever I visit this form, the existing saved consortium does not show up as selected. What do I need to change about my select expression in order that it properly saves the consortium?

I am properly populating consortium when supplying the data from the planet model, so in the browser, you do in fact get all the above data. It's there. But I just don't know what I'm supposed to do with this select expression. Any help would be most appreciated!!

Problem courtesy of: CommaToast

Solution

select(ng-model='form.consortium._id', name='consortium._id', ng-options="consortium._id as consortium.name for consortium in consortia")

Worked perfectly.

Solution courtesy of: CommaToast

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to deal with nested data in Angular.js select in a form?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×