1

I have the following example.json file:

[
  {
    "interests": [
      {
        "item": "art"
      },
      {
        "item": "literature"
      },
      {
        "item": "history"
      },
      {
        "item": "science"
      }
    ]
  },
  {
    "experience": [
      {
        "year": "novice"
      },
      {
        "year": "experienced"
      }
    ]
  }
]

and in my view controller, I read the file like this:

app.controller('ProfileCtrl', ["$scope", "$state", "$http",
    function ($scope, $state, $http) {

    $http.get('files/example.json').success(function (data)
        {
            $scope.myjsonobj= data;
        });

and in my html view, I am injecting the values like this:

<div ng-controller="ProfileCtrl">
...
    <select class="form-control" ng-model="user.favs">
       <option ng-repeat="p in interests.myjsonobj" value="{{p.item}}">{{p.item}}</option>
    </select>

question:

How can I show only the list of values of "interests" in my dropdown menu? How can I access the nested json array in angularjs?
my current setup is not working.

1
  • You can set $scope.myjsonobj to data.interests instead of to the entire JSON reply Commented Oct 17, 2016 at 21:18

2 Answers 2

1

assuming 'interests' is your controller shortname.

you can use:

<option ng-repeat="p in myjsonobj[0].interests" value="{{p.item}}">{{p.item}}</option>

Ideally, you should loop the data object to find the index with the 'interests' key vs hardcoding [0].

update: removed "interests." from the repeat. doesn't seem like you have the ctrl shortname binding.

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

6 Comments

what do you mean by controller shortname? I updated my controller name in my question, trying to avoid name confusions.
ng-controller="YourControllerName as interests" this binds methods and properties of your controller to "interests". with that in the html, you access scope variable via interests.scopeVariable.
if you don't have setup, then just change it to this: <option ng-repeat="p in myjsonobj[0]" value="{{p.item}}">{{p.item}}</option>
could you please look at my updated question and provide an adapted answer please. I am confused now with different terminologies.
can you show me the html element where you have the ng-controller binded? e.g. <div ng-controller="ProfileCtrl...">
|
0

I'm not an angular guy. But with my javascript knowledge it seems to me that the

"interests.myjsonobj"

should be

"myjsonobj.interests"

Comments

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.