7

I have the following in my HTML file:

    <td style="width: 200px;">
        <span ng-repeat="list in listGroups">
            <label for="{{ list.description }}" />{{ list.description }}</label>
            <input ng-model="$parent.listGroup" id="listGroup-{{ list.value }}"  name="listGroups" value="{{ list }}" type="radio" />
        </span>
   </td>

The listGroups contains:

[
    {
        "description": "New by Territory",
        "group": "product",
        "type": "new"
    },
    {
        "description": "New by Genre",
        "group": "genre",
        "type": "new"
    },
    {
        "description": "Charts by Territory",
        "group": "product",
        "type": "chart"
    },
    {
        "description": "Charts by Genre",
        "group": "genre",
        "type": "chart"
    }
]

When I click a radio button the listGroup (set in ng-model) becomes, for example:

{"description":"New by Genre","group":"genre","type":"new"}

When I look at listgroup with typeof $scope.listGroup, I see that it is a string now!

As such, I can't access it's properties in the other bindings in the rest of the HTML page.

In this case, I want ng-show="listGroup.group == 'genre'"

What's happening here and, more importantly, how do I make it do what I want it to do, which is keep the object as an object?

Thanks all

Dave

4 Answers 4

4

The problem is that the input's value attribute only accepts strings, not objects (check here). When you do this: value="{{ list }}", you are basically doing input.value = list.toString().

One possible workaround is to use the $index of the ng-repeat directive. Example: http://jsfiddle.net/bmleite/cKttd/

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

1 Comment

Also, don't use $parent., it can bring you problems in the future.
3

It is now possible to use ng-value.

Example

<input ng-model="$parent.listGroup" id="listGroup-{{ list.value }}" name="listGroups" ng-value="{{ list }}" type="radio" />

Comments

1

Why do you want to have an object as an ng-model? That is what's causing your listGroup variable to become a string, since ng-model only works with strings. If you have a complex object that you want to use with ng-model, you should create a directive and implement a parser and a formatter, like this: How to do two-way filtering in angular.js?

Comments

0

You could also JSON.parse(object-in-string) , but I think I like @bmleite solution better. Just throwing this out there -- incase someone has a different use-case.

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.