model update for dynamic generated radio buttons
Asked Answered
C

1

6

please can someone help me with this little binding mess. I'm trying to generate a list of tasks

here is the model definition in my TaskController:

angular.module('yeomantestApp')
  .controller 'TaskController', ($scope) ->
    $scope.currentTask
    $scope.tasks = [
        {
            id: 1
            name: 'write test'
            elapsedTime: 15
        },
        {
            id: 2
            name: 'run test'
            elapsedTime: 32
        },
        {
            id: 3
            name: 'write code'
            elapsedTime: 22
        }
    ]

So, now I want to render the model with the following view. The view iterates over the task array and build a list of radio buttons for each task. My problem is, that the model binding to currentTask is somehow not working. When I select any task the currentTask model entry is not updating. But according the tutorials and documentation it should.

<div class="hero-unit" ng-controller="TaskController">
    <h1>Tasks</h1>
    <h2>current {{currentTask}}</h2>
    <form name="taskForm">
        <div ng-repeat="task in tasks">
            <input type="radio" name="taskGroup" ng-model="currentTask" value="{{task.id}}">{{task.name}} {{task.elapsedTime}}
        </div>
    </form>
</div>
Caines answered 2/11, 2012 at 16:17 Comment(0)
P
12

Changing ng-model attribute to ng-model="$parent.currentTask" should solve your problem.

Here is the jsFiddle: http://jsfiddle.net/dp3xq/8/

Pleo answered 2/11, 2012 at 16:32 Comment(3)
you are my hero!!!! i should have used the scope debugging feature right away THX a lotCaines
Or better yet blog.angularjs.org/2012/07/introducing-angularjs-batarang.html to track down scope-related issues :-)Pleo
See also #12745288, where two other "nested scope" solutions are presented: 1) use a controller method to modify currentTask 2) make currentTask a non-primitive type, so that the ng-repeat scopes get a reference to it (instead of a local copy).Intuitivism

© 2022 - 2024 — McMap. All rights reserved.