0

I want do a dependen select / dropdown.

I obtain this json sample:

[
  {
    "id": 15695,
    "username": "user1",
    "address": {
      "id": 16794,
      "location": "O Treviño de San Pedro"
    },
    "jobs": [
      {
        "id": 7562,
        "name": "ut"
      },
      {
        "id": 7565,
        "name": "temporibus"
      },
      {
        "id": 7603,
        "name": "perspiciatis"
      },
      {
        "id": 7622,
        "name": "optio"
      }  
    ]
  }
]

This is the angular code:

    <select ng-model="industrialist.user" ng-options="user.id as user.username for user in users"></select>
    <select ng-model="industrialist.job" ng-options="job.id as job.name for job in industrialist.user.jobs"></select>

If I do this code, then dependent select works but then i haven't de correct key in industrialist.user.

        <select ng-model="industrialist.user" ng-options="user.username for user in users"></select>

What can I do?

Thanks

2
  • confusing...you have 2 versions of ng-model="industrialist.user" but different ng-options. Please explain issue in more detail Commented Nov 21, 2013 at 16:12
  • Yes the problem, is that in second version the dependent select works but then I haven't user.id as key and I need this key. Commented Nov 21, 2013 at 16:15

1 Answer 1

1

I believe what you need to do is save the whole user rather than the just the id. You can get the id out later if you need it.

<select ng-model="industrialist.user" ng-options="user.id as user.username for user in users"></select>
<select ng-model="industrialist.job" ng-options="job.id as job.name for job in industrialist.user.jobs"></select>

Here is a working fiddle. JSFiddle

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is then in form send all user and I need only user.id
@MarcMoralesValldepérez I changed the fiddle, it works just like you wanted, I think. Yeah?
Yes in fiddle works but there are this error: Cannot read property 'user' of undefined. First time $scope.industrialist.user is undefined.
Use an ng-init or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.