简体   繁体   中英

AngularJS how to recover answers from an html file

Good morning, I have started AngularJS like 2 days ago and I'm trying to do an exercice which is a form with three questions (for now) each question have 3 answers and each answer have a score which determines after a calculation to which category the user answering belongs.

What I have done till now:

 app = angular.module('app', []); app.controller('qcmController', function($scope) { $scope.categories = [{ name: 'humain', description: 'categorie humain', score: 0 }, { name: 'Dev', description: 'categorie Developpeur', score: 0 }, { name: 'Nos Nos', description: 'categorie Mis-dev,Mis-humain', score: 0 } ]; $scope.qst = [{ label: 'Aimez-vous Ruby ?', rep: [{ label: 'j"adore', categorie: 'dev', point: '10' }, { label: 'oui', categorie: 'Nos Nos', point: '10' }, { label: 'C"est quoi Ruby ?', categorie: 'humain', point: '10' } ] }, { label: 'c"est quoi Java ?', rep: [{ label: 'Langage de programation', categorie: 'dev', point: '10' }, { label: 'Langage', categorie: 'Nos Nos', point: '10' }, { label: '9ahwa :) ?', categorie: 'humain', point: '10' } ] }, { label: 'Vous aimez la programation?', rep: [{ label: 'j"adore', categorie: 'dev', point: '10' }, { label: 'oui', categorie: 'Nos Nos', point: '10' }, { label: 'c"est quoi la programation ?', categorie: 'humain', point: '10' } ] }, ]; }); 
 <script src="https://code.angularjs.org/1.4.6/angular.min.js"></script> <div ng-app="app"> <div ng-controller="qcmController"> <ul> <li ng-repeat="cat in categories"> <b ng-bind="cat.name"></b> </li> </ul> <form ng-submit=""> <ul> <li ng-repeat="question in qst"> <b ng-bind="question.label"></b> <ul> <li ng-repeat="rep in question.rep"> <input type="checkbox"> <span ng-bind="rep.label"></span> </li> </ul> </li> </ul> <input class="btn-primary" type="submit" value="add"> </form> </div> </div> 

My problem is I have no idea how can I recover the answer so I can calculate that score.

Using ng-model on the checkbox you can store a selected boolean. Check out the console log object of the item you select when submitting:

<form ng-submit="calc()">
<ul>
    <li ng-repeat="question in qst">
        <b ng-bind="question.label"></b>
        <ul>
                <li ng-repeat="rep in question.rep">
                    <input type="checkbox" ng-model="rep.selected"> <span ng-bind="rep.label"></span>
                </li>
            </ul>
    </li>
</ul>
<input class="btn-primary" type="submit" value="add">

app.js

app=angular.module('app', []);
app.controller('qcmController', function($scope){
    $scope.categories = 
    [
        {name: 'humain', description:'categorie humain', score: 0},
        {name: 'Dev', description:'categorie Developpeur', score: 0},
        {name: 'Nos Nos', description:'categorie Mis-dev,Mis-humain', score: 0}
    ];


    $scope.qst = 
    [
        {label:'Aimez-vous Ruby ?', rep :[{label:'j"adore', categorie:'dev', point:'10'},
                                        {label:'oui', categorie:'Nos Nos', point:'10'},
                                        {label:'C"est quoi Ruby ?', categorie:'humain', point:'10'}  ]},

        {label:'c"est quoi Java ?', rep :[{label:'Langage de programation', categorie:'dev', point:'10'},
                                        {label:'Langage', categorie:'Nos Nos', point:'10'},
                                        {label:'9ahwa :) ?', categorie:'humain', point:'10'}  ]},
        {label:'Vous aimez la programation?', rep :[{label:'j"adore', categorie:'dev', point:'10'},
                                                    {label:'oui', categorie:'Nos Nos', point:'10'},
                                                    {label:'c"est quoi la programation ?', categorie:'humain', point:'10'}  ]},
    ];

    $scope.calc = function () {
        console.log($scope.qst);
    };
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM