0

I have a webapp dedicated to TV Shows. A controller fetches the registered TV Shows:

$http.get('/shows').success(function(data) {

    $scope.shows = data; 

    var temp = data[0];

    $scope.radio-input-shows = temp['name'];

});

This is passed to an AngularJS view, which displays them with a radio button.

<div class="col-use-list sidebar">

    <div class="col-md-12 sidebar-element sidebar-triangle" ng-repeat="show in shows" style="margin-bottom: 10px;">

        <div class="col-use-icon" style="padding-left: 0px;">

            <img alt="{{ show.name }}" src="img/artwork/{{ show.image_name }}.png" style="width:60px;height:60px;">

        </div>

        <div class="col-use-name">

            <p>{{ show.name }}</p>

        </div>

        <div class="col-use-select">

            <input type="radio" name="sidebar-radio" ng-model="radio-input-shows" value="{{ show.image_name }}">

        </div>

    </div>

    <tt>Show = {{radio-input-shows | json}}</tt><br/>

</div>

What I need now is to pass the name of a show as a value to the radio input. Is this even possible?

=================

So I now changed the input to this <input type="radio" name="checkbox" ng-model="tvshow" ng-value="show.name">. And changed this <tt>Show = {{tvshow}}</tt><br/>. However, it's still not updating if I click on another radio button... `

1
  • 1
    you are using primitives for variables assigned to ng-model. Since ng-repeat creates child scopes you will have inheritance problems. Always have a dot in ng-model which means passing object properties not primitives Commented Sep 19, 2014 at 18:05

1 Answer 1

1

Of course! You simply use ng-value to set the value according to an Angular expression, and use a $scope property.

<input type="radio" ng-model="whatever" ng-value="show.name">

Live Demo

You're also using invalid property names. radio-input-shows is not valid JavaScript and is throwing an error. That should camelCased radioInputShows or at least something that isn't a syntax error.

Lastly, you should always follow the "dot" rule so that child scope changes affect parent scopes.

<input type="radio" ng-model="foo.radioInputShows" ng-value="show.image_name">
<tt>Show = {{foo.radioInputShows | json}}</tt><br/>

In the controller:

$scope.foo = {
  // default value
  radioInputShows: '123'
};

This is just taking advantage of object references to share changes.

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

4 Comments

I did that, but I if I click a radio-button <tt>Show = {{radio-input-shows | json}}</tt><br/> doesn't update...
@User183849481 Works fine when you don't have syntax errors: jsbin.com/xazodu/2/edit Be sure to look at the console while developing. It should have been showing you that the variable name was not acceptable.
I updated the question, since it's still not updating :S
@User183849481 Ah, I didn't catch it at first due to the two other problems. See my updated answer. Just another note: inline style is almost never a good idea. There are occasionally good uses of ngStyle to conditionally bind in something, but that's about it.

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.