{"id":1975,"date":"2015-07-14T04:09:55","date_gmt":"2015-07-14T04:09:55","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1975"},"modified":"2015-07-31T09:53:21","modified_gmt":"2015-07-31T09:53:21","slug":"reference-the-inputs-created-with-ng-repeat-in-angular-based-on-the-number-of-variable-options","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/reference-the-inputs-created-with-ng-repeat-in-angular-based-on-the-number-of-variable-options\/","title":{"rendered":"Reference the inputs created with ng-repeat in angular based on the number of variable options"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1975]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/21605264\/reference-the-inputs-created-with-ng-repeat-in-angular-based-on-the-number-of-va\">question<\/a> was:<\/p>\n<p>The problem I&#8217;m facing, as outlined in the code, is that I can&#8217;t seem to find out how to bind the second checkboxes (created with ng-repeat) to some model which I would then be able to use further down the code (showing\/hiding yet another set of options). Also, I managed to show the additional number of inputs based on the <code>count<\/code> parameter in the <code>$scope.availableOptions<\/code> by using the <code>$scope.getItterator<\/code> function, but now I don&#8217;t see how would I access the values of these checkboxes? I know I have to somehow use the option &#8220;ng-model&#8221; but am failing to do so, so any help appreciated.<\/p>\n<p>My code is <a href=\"http:\/\/jsfiddle.net\/CkHhJ\/25\/\" rel=\"nofollow\">here<\/a>, but am showing it here too:<\/p>\n<p>HTML:<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-controller='MyCtrl'&gt;\r\n    Show additional options &lt;input type=\"checkbox\" ng-model=\"additionalOptions\"&gt;\r\n    &lt;div ng-show=\"additionalOptions\"&gt;\r\n        &lt;ul&gt;\r\n            &lt;li ng-repeat=\"option in availableOptions\"&gt;\r\n                &lt;label class=\"checkbox\" for=\"opt_{{option.id}}\"&gt;\r\n                    &lt;input type=\"checkbox\" name=\"opt_{{option.id}}\" id=\"opt_{{option.}}\" \/&gt;\r\n                    {{option.name}}\r\n\r\n                    &lt;ul ng-show=\"if the upper checkbox is clicked\"&gt;\r\n                        &lt;li&gt;\r\n                            &lt;input type=\"text\" ng-repeat=\"i in getItterator(option.count)\" ng-model=\"SOME_VALUE_TO_BE_USED_BELOW\"\/&gt;\r\n                            Output the value based on what's entered above in the textboxes (SOME_VALUE_TO_BE_USED_BELOW)\r\n                        &lt;\/li&gt;   \r\n                    &lt;\/ul&gt;\r\n                &lt;\/label&gt;\r\n            &lt;\/li&gt;\r\n        &lt;\/ul&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>and my js:<\/p>\n<pre class=\"lang:default decode:true\">function MyCtrl($scope) {\r\n    $scope.availableOptions = [{\"id\":\"1\",\"name\":\"easy\",\"count\":\"2\"},{\"id\":\"2\",\"name\":\"medium\",\"count\":\"3\"},{\"id\":\"3\",\"name\":\"hard\",\"count\":\"2\"}];\r\n\r\n    $scope.getItterator=function(n){\r\n        var a = new Array();\r\n\r\n        for(var i=1; i &lt;= n; i++)\r\n            a.push(i);\r\n\r\n        return a;       \r\n    };\r\n}<\/pre>\n<p>The answer, by user <a href=\"http:\/\/stackoverflow.com\/users\/1957251\/khanh-to\">Khanh TO<\/a>, was:<\/p>\n<blockquote><p>Try <code>ng-model=\"option.checked\"<\/code>:<\/p>\n<pre class=\"lang:default decode:true\">&lt;input type=\"checkbox\" name=\"opt_{{option.id}}\" id=\"opt_{{option.}}\" ng-model=\"option.checked\"\/&gt;<\/pre>\n<p>And then you can access this property further below like this:<\/p>\n<pre class=\"lang:default decode:true\">&lt;ul ng-show=\"option.checked\"&gt;<\/pre>\n<p><a href=\"http:\/\/jsfiddle.net\/xxRy3\/\" rel=\"nofollow\">DEMO<\/a><\/p>\n<p>If you need to also reference the text box inputs. You create a new property (<code>values<\/code>) on the option. Like this:<\/p>\n<pre class=\"lang:default decode:true\">{\"id\":\"1\",\"name\":\"easy\",\"count\":\"2\",values:[]}<\/pre>\n<p>Html:<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-repeat=\"i in getItterator(option.count)\"&gt;\r\n     &lt;input type=\"text\"  ng-model=\"option.values[$index]\"\/&gt;\r\n      {{option.values[$index]}}\r\n&lt;\/div&gt;<\/pre>\n<p><a href=\"http:\/\/jsfiddle.net\/xxRy3\/1\/\" rel=\"nofollow\">DEMO<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,35],"tags":[],"class_list":["post-1975","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1975"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1975\/revisions"}],"predecessor-version":[{"id":1977,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1975\/revisions\/1977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}