0

I am trying to access the value 1 like so.

var m = {
    {"name":"name"},
    {"contents":[{'one':1,'two':2}]}
};

function MyCtrl($scope) {
    $scope.items = m;
    $scope.contents = m.contents;
}

{{contents.one}}
{{items.contents.one}}
<li ng-repeat="item in items">{{item.one}}{{item.two}}</li>

None of them works. Please help me out.

1 Answer 1

2

m is an array and arrays don't have object properties.

The property contents exists in the second element of the array.

Try:

$scope.contents = m[1].contents;

It would seem that the preffered structure you would want would be an object not array:

var m = {
    "name":"name",
    "contents":[{'one':1},{'two':2}]
};
Sign up to request clarification or add additional context in comments.

21 Comments

Is there any way I can access the element without index? I mean, with specific key?
Also your contents array should use the same property names in each object
I am sorry that I wrote '[' instead of '{'. I intended to talk about a json object that has an array inside. How do you do it if it is an object?
how do you do what? Changing the outer braces you just created an invalid object
I mean if I have a json object that has an array inside, how do I access the array item with property name? If I have a json object {name:'name',contents:[{'one":1},{'two":2}], how do I access 'one'?
|

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.