Radio Buttons - 1.0.0rc10

http://angularjs.org/

by programmieraffe

HTML

<script src="http://code.angularjs.org/1.0.0rc10/angular-1.0.0rc10.js"></script>

<div ng:app>
<div    ng:controller="TestCtrl">
    <button ng:click="loadAnswers()">Load answers</button>
    <button ng:click="addAnswer()">Add answer</button>
<p>&nbsp;</p>
    <ul>
        <li ng:repeat="answer in json_object_decoded.answers">
           <input type="text" ng:model="answer.text"/>
           <input type="radio" ng:model="answer.correct" ng:change="updateAnswers($index)" ng:format="boolean" value="true" />
        </li>
    </ul>
    <p>&nbsp;</p>
    
    
<p>Result:     
    <span ng:bind="json_object_decoded"></span>    </p>
    
    
    <button ng:click="submit()">Save changes</button>
</div>

<br />
<br />
</div>

JavaScript

TestCtrl = function($scope) {
    $scope.updateAnswers = function(idx) {
        angular.forEach($scope.json_object_decoded.answers, function(answer, i) {
            answer.correct = idx === i;
        });
    };

    $scope.json_object_decoded = {};



    $scope.loadAnswers = function() {
        // this is loaded from db normally
        var fromServer = angular.toJson({
            'answers': [{
                'text': 'Answer 1',
                'correct': true},
            {
                'text': 'Another Answer 2',
                'correct': false}]
        });
        $scope.json_object_decoded = angular.fromJson(fromServer);
        $scope.json_object_decoded.answers[0].correct = true;
    }

    $scope.addAnswer = function() {
        console.log('add answer');
        $scope.json_object_decoded.answers.push({
            'text': 'New',
            'correct': false
        });
    }

    $scope.submit = function() {

    }

    $scope.loadAnswers();

}