javascript - How do I get the value of a user-selected radio button in the controller? -


I'm writing a simple survey app in AngularJs, and want to capture the result of the radio button selection, one inside the controller Array I have an array of question objects, one field of each body and one choice field in which there are string values ​​for the option. I am trying to force the value of the radio button selected with the 'ng-model' attribute. I can not get this value from within the controller.

Here is the index.html file:

  & lt ;; Doctype html & gt; & Lt; html ng-app = "estimatorApp" & gt; & Lt; Top & gt; & Lt; Script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js" & gt; & Lt; / Script & gt; & Lt; Script src = "js / controllers.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; body ng-controller = "QuestionListCtrl" & gt; {{FAQ}} & lt; Br / & gt; & Lt; Ul & gt; & Lt; Li ng-repeat = "Options [q_counter] .choices" in the questions. & Lt; Input type = "radio" ng-model = "answer" ng-click = "next ()" \ ng-value = "name" name = "answer" & gt; {{Option}} & lt; Br / & gt; & Lt; / li & gt; & Lt; / Body & gt; & Lt; / Html & gt;   

And here is the 'admin.js' file:

  var estimate App = angular.module ('estimatorApp', []) estimatorApp.controller ( 'Question: List' Ctrl, Function ($ scope) {$ scope.questions = [['body': 'What kind of app do you want to create?', 'Options': ['iOS', 'Android', 'Mobile' Web ']}, {' body ':' have you already started development? ',' Option ': [' yes', 'no']}] $ scope.q_counter = 0; $ scope.question = $ scope.questions [$ scope.q_counter] ['body']; $ scope.TarS = [] $ scope.answer = "" $ scope.next = function () {if ($ scope.q_counter <$ scope.questions.length- 1) {console.log ("answer : "+ $ scope.answer); // $ scope.answer is undefined $ scope.q_counter + = 1; $ scope.question = $ scope.questions [$ scope.q_counter] ['body'];} and {console} .log ($ scope.answers);}}});   

Once I am able to capture the result of an input, I will push it at the end of the $ radius. To store it's tower arrays.

If you want you can use the directly answer array you have There is already an index to use with q_counter , so it is quite straightforward.

Whatever things I've added, the value with ng-model = "answers [q_counter]" and value = "{{choice}}" Setting up

Here's one.

JS: var app = angular Module ('myApp', []); App.controller ('questionListCtrl', function ($ scope) {$ scope.questions = [{'body': 'What kind of app do you want to create?', 'Options': ['iOS', 'Android' 'Mobile' ']}, {' body ':' have you already started development? ',' Option ': [' yes', 'no']}] $ scope.q_counter = 0; $ scope .question = $ scope.questions [$ scope.q_counter] ['body']; $ scope.TarS = [] $ scope.answer = "" $ scope.next = function () {if ($ scope.q_counter & lt ; $ scope questions.length-1) {console.log ($ scope.answers); // $ scope.answers is the full list of answers $ scope.q_counter + = 1; $ scope.question = $ scope.questions [$ Scope. Q_counter] ['body'];} and {console.log ($ scope.answers);}}});

HTML:

  & lt ;! DOCTYPE html & gt; & Lt; Html ng-app = "myApp" & gt; & Lt; Top & gt; & Lt; Script Data - security = "angular.js@*" Data-save = "1.3.0-beta.5" src = "https://code.angularjs.org/1.3.0-beta.5/angular js" & gt; & Lt; / Script & gt; & Lt; Link rel = "stylesheet" href = "style.css" /> & Lt; script src = "script.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; body ng-controller = "QuestionListCtrl" & gt; & Lt; H1 & gt; Hello Plunker! & Lt; / H1> {{FAQ}} & lt; Br / & gt; & Lt; Ul & gt; & Lt; Li ng-repeat = "Options [q_counter] .choices" in the questions. & Lt; Input type = "radio" ng-model = "answer [q_counter]" ng-change = "next ()" value = "{{likes}}" name = "answer" & gt; {{Option}} & lt; Br / & gt; & Lt; / li & gt; & Lt; / Ul & gt; & Lt; / Body & gt; & Lt; / Html & gt;    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -