0

From this json array I need to build dynamic radio buttons with mobile numbers in angularjs:

challengeSelectInfo:
[
    {"mob_0" : "xxxxx1211"},
    {"mob_1" : "xxxxx1211"},
    {"mob_2" : "xxxxx1211"}
]

I tried ng-repeat and iterate over challengeSelectInfo but the issue that I'm facing is keys(mob_0,mob_1,mob_2) are different and I'm unable to generate dynamic radio buttons.

Any help is appreciated.

Thanks

3 Answers 3

1

You need to specify keys for your arrays :

$scope.newArr = [];
angular.forEach(challengeSelectInfo, function(val, key) {
    /* do something for all key: value pairs */
     $scope.newArr.push({id: key, value: val});
});

Then loop through newArr arrays and assign to radio button :

<input name="{{item.value}}" type="radio" ng-model="item.id" value="{{item.value}}">
Sign up to request clarification or add additional context in comments.

1 Comment

hi, thanks. can you give me a working js fiddle for the same. like what I need to put as key and value. sorry for silly questions
0

made this simply for your understanding purpose hope you can achieve your requirement by using this

  var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.challengeSelectInfo= [
    {"mob_0" : "xxxxx1211"},

{"mob_1" : "xxxxx1211"},

{"mob_2" : "xxxxx1211"} ];


});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
   <div ng-repeat="(k,v) in challengeSelectInfo ">
     <div ng-repeat="(x,y) in v">
      <input type="radio" />{{y}}
     </div>
   </div>
   </body>
  

</html>

6 Comments

after using this m getting ng-repeat:dupes error :(
use track by $index option in ng-repeat
thanks. track by $index is working but it's giving entire challengeSelectInfo data as one one radio button. for every spaces, commas everything it gives a radio button. how to rectify that?
can u give me a plunker with currently what your seeing in output.....and to resolve this you need to track it by a unique property like keys here.....
hey, m sorry I can't provide what output m getting as my codebase is in virtual machine. you just let me know {{y}}, here what to write and how to track it by a unique properties. m totally new to this.
|
0

Can you check this JSFIDDLE Example

  var data = {  "challengeSelectInfo" : [ {"mob_0" : "xxxxx1211"},
   {"mob_1" : "xxxxx1211"}, {"mob_2" : "xxxxx1211"} ]}
  $scope.radioGrp = (data['challengeSelectInfo'] || []).map(function(obj){      
       for(var i in obj){ 
          return { 'value': i, 'name': obj[i] }; 
       } 
     });

 <input type="radio" ng-repeat="rb in radioGrp" ng-value="rb.value" ng-modal="radioValue" name="{{rb.name}}">

Comments

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.