0

I need to display selected radio button in the response div,if no option is selected that div will disappear.

View Code:

<body ng-app="QuestionDisplayModule">
    <form>
        <div ng-controller="QuestionDisplayController">
            <div ng-repeat="q in questionsData" style="border:groove 1px green">
                <h4 style="color: darkmagenta">{{q.QText}}</h4>
                <p ng-repeat="a in q.answer1" style="color: cadetblue"><input type="radio"  name="answers" ng-model="options" value="{{a.option1}}" ng-checked="optionselected(options)" />{{a.option1}}</p>
                <div>your answer is :{{selectedoption}}</div>
            </div>
        </div>
    </form>
</body>

Script Code:

<script src="~/Scripts/angular.min.js"></script>
    <script>
        var myApp = angular.module("QuestionDisplayModule", [])
        .controller("QuestionDisplayController", function ($scope, $http, $log) {

            $scope.optionselected = function (options)
            {
                $scope.selectedoption = options;
            }
            $http.get("displayQuestion").then(function(response)
            {
                $log.info(response);
                $scope.questionsData = response.data;
            })
        })
    </script>
3
  • 1
    You can just display "your answer is : {{options}}" Commented Jun 30, 2016 at 11:19
  • any error showing? Commented Jun 30, 2016 at 11:31
  • no its not showing any error that is problem @Tanisha Commented Jun 30, 2016 at 11:33

3 Answers 3

1

You should use different name for each question otherwise you will can select only one option for all question and and should use different model for each question and no need to use ng-checked="optionselected(options)" to set selected option value because of you used ng-model.

you can try like this

<div ng-controller="QuestionDisplayController">
      <div ng-repeat="q in questionsData" style="border:groove 1px green">
        <h4 style="color: darkmagenta">{{q.QText}}</h4>
        <p ng-repeat="a in q.answer1" style="color: cadetblue">
          <input type="radio" name="answers+{{$parent.$index}}" ng-model="q.selectedAns" value="{{a.option1}}" />{{a.option1}}</p>
        <div ng-show="q.selectedAns">your answer is :{{q.selectedAns}}</div>
      </div>
    </div>

PLUNKER DEMO

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

3 Comments

thank you @shaishab roy. am searching for this since 4 hours
colud you please suggest me how to pagination using angularJs @shaishab roy
you can use angular ui bootstrap to implement pagination easily angular-ui.github.io/bootstrap
0

Use ng-show/ng-hide to display the div accordingly . Like

 <div ng-show="selectedoption != null">your answer is :{{selectedoption}}</div>

You can also use ng-if . Also you can check if selectedoption is blank .

Read :- https://docs.angularjs.org/api/ng/directive/ngShow

6 Comments

first i need to display the selected option value that is not working fine @neda
<div ng-show="selectedoption != null">your answer is :{{selectedoption}}</div> if i use the when i select option for one question it will show for all questions with same option value @neda
Then you have to give scope value of the checkbox different for each repeat. try changing as ng-model="q.options" and then do <div ng-show="q.options != null"
how i need to pass model value to controller <p ng-repeat="a in q.answer1" style="color: cadetblue"><input type="radio" name="answers" ng-model="{{a.option1}}" value="{{a.option1}}" ng-click="optionselected()" />{{a.option1}}</p> @neda
You dont need curly braces here just ng-model="a.option1"
|
0

You can simply do like did in the following fiddle.

http://jsfiddle.net/vishnusuresan/Lvc0u55v/6247/

simply use

  <div ng-controller="MyCtrl">
 <body >
  <form>
    <div >
        <div  style="border:groove 1px green">
            <h4 style="color: darkmagenta"></h4>
            <p style="color: cadetblue"><input type="radio"  name="answers" ng-model="options" value="value1" ng-checked="optionselected(options)" /></p>
            <div>your answer is :{{options}}</div>
        </div>
    </div>
</form>

I don't know what you want to accomplish.

1 Comment

In view list of questions with options i.e one question with 4 options so when i select one option form the respective question that answer should display in "your answer is": like @Tanisha

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.