Nested directives wtth radio buttons and ng-model

64 views
Skip to first unread message

Brad Rice

unread,
Nov 23, 2015, 11:49:20 AM11/23/15
to AngularJS
I'm trying to create a directive that is a multiple choice screen. I want the radio button of the correct answer to be selected as this will be an edit screen for the end user to show the correct answer. When I do ng-repeat inside of another ng-repeat, only the last set of answers get's it's radio button selected. I'm not sure how to fix this problem I've created a plunker to display the issue:


Any help would be appreciated.Thanks

Sander Elias

unread,
Nov 24, 2015, 12:18:26 AM11/24/15
to AngularJS
Hi Brad,


I expect some questions from you on the how and why ;)

Regards
Sander

Brad Rice

unread,
Nov 24, 2015, 8:17:47 AM11/24/15
to AngularJS
I'm trying to check the correct answer which is an array index value. So for example, the correct answer in the first question is "3", so the last radio button is should be selected.I am using $index of the ng-repeat to set the radio button value.

Your logic won't work, because the check is not on equality of the answer === correct, but the array number.

Sander Elias

unread,
Nov 24, 2015, 10:59:26 AM11/24/15
to AngularJS
Hi Brad,

It does exactly what you are asking doesn't it? If you look behind the question, there is a true/false that updates when you are selecting the question. Isn't that providing the correct answer?
If not, please explain what it is you are trying to accomplish, because apparently somehow I do misunderstand your issue.

Regards
Sander 

Brad Rice

unread,
Nov 24, 2015, 11:05:42 AM11/24/15
to AngularJS
Thank for the help Sander. I have that part working on another screen. The part I need to work is the edit screen. The page I am working on is an edit screen for end users to create questions and answers. I want them to pull up a page of already entered questions and answers and have the radio button set to the one that was already set in the data as the correct answer. This is not a quiz page that is challenging the user to find the right answer. For some reason I can't get the radio button to be selected when the page loads on the correct answer.

Sander Elias

unread,
Nov 24, 2015, 11:20:10 AM11/24/15
to AngularJS

Brad Rice

unread,
Nov 24, 2015, 11:23:21 AM11/24/15
to ang...@googlegroups.com
Yes, what did you do? It doesn't look different than my original code.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/zOiEpRtDj50/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Brad Rice
brad...@gmail.com
--
“Live as if you were to die tomorrow. Learn as if you were to live forever.”
― Mahatma Gandhi

Sander Elias

unread,
Nov 24, 2015, 11:31:04 AM11/24/15
to AngularJS
Hi,

I introduced a ngFrom tag, and removed the 'name' property. That way, angular takes care of 'creating button-groups'

Regards
Sander

Brad Rice

unread,
Nov 24, 2015, 11:45:13 AM11/24/15
to ang...@googlegroups.com
Sweet! Thanks so much.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/zOiEpRtDj50/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Vaibhav Gupta

unread,
Nov 25, 2015, 1:12:53 AM11/25/15
to AngularJS
Hi Sander,

Is ng-form required? In Brad's original plunk by just removing the name attribute the intended result is achieved. I think ng-model is taking care of making radio buttons as a group.

Regards,

Vaibhav Gupta

Sander Elias

unread,
Nov 25, 2015, 6:03:27 AM11/25/15
to AngularJS
HI Vaibhav,

I started off by inserting it , because when you create rows in a repeat, you need it if you want to do some validation. In this particular case it might not be really needed, but once you add something as validation, it's not so optional anymore.

Regards
Sander 
Reply all
Reply to author
Forward
0 new messages