AngularJS radio button groups with name attribute not initializing properly











up vote
0
down vote

favorite












The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.



Please refer to the example below:






var m = angular.module("MyApp", );

m.controller("MyController", ["$scope", function($scope) {

$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);

body {
background-color: #1D1F20;
}

.row {
margin-bottom: 15px;
}

.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">

<span>{{eachItem.name}} => </span>

<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>

<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>

<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>

</div>
</div>












share|improve this question




















  • 1




    Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
    – Mickers
    Nov 19 at 17:31










  • Awesome. That worked. Thanks @Mickers.
    – Manoj Shrestha
    Nov 19 at 17:37















up vote
0
down vote

favorite












The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.



Please refer to the example below:






var m = angular.module("MyApp", );

m.controller("MyController", ["$scope", function($scope) {

$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);

body {
background-color: #1D1F20;
}

.row {
margin-bottom: 15px;
}

.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">

<span>{{eachItem.name}} => </span>

<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>

<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>

<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>

</div>
</div>












share|improve this question




















  • 1




    Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
    – Mickers
    Nov 19 at 17:31










  • Awesome. That worked. Thanks @Mickers.
    – Manoj Shrestha
    Nov 19 at 17:37













up vote
0
down vote

favorite









up vote
0
down vote

favorite











The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.



Please refer to the example below:






var m = angular.module("MyApp", );

m.controller("MyController", ["$scope", function($scope) {

$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);

body {
background-color: #1D1F20;
}

.row {
margin-bottom: 15px;
}

.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">

<span>{{eachItem.name}} => </span>

<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>

<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>

<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>

</div>
</div>












share|improve this question















The radio buttons group doesn't seem to initialize property inside a ng-repeat block if we specify the name attribute to the group. It seems working for the last set of radio groups, but not for the rest. Also, it works fine if I remove the name attribute. Per [AngularJS documentation][1], the name attribute is not needed if we use it with ngModel. But it doesn't say we must not use it. And I need to specify a name attribute.



Please refer to the example below:






var m = angular.module("MyApp", );

m.controller("MyController", ["$scope", function($scope) {

$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);

body {
background-color: #1D1F20;
}

.row {
margin-bottom: 15px;
}

.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">

<span>{{eachItem.name}} => </span>

<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>

<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>

<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>

</div>
</div>








var m = angular.module("MyApp", );

m.controller("MyController", ["$scope", function($scope) {

$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);

body {
background-color: #1D1F20;
}

.row {
margin-bottom: 15px;
}

.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">

<span>{{eachItem.name}} => </span>

<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>

<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>

<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>

</div>
</div>





var m = angular.module("MyApp", );

m.controller("MyController", ["$scope", function($scope) {

$scope.myArr = [{
name: "Obj 1",
status: "a"
}, {
name: "Obj 2",
status: "b"
}, {
name: "Obj 3",
status: "c"
}];
}]);

body {
background-color: #1D1F20;
}

.row {
margin-bottom: 15px;
}

.demo {
background: green;
color: white;
padding: 10px;
border-radius: 3px;
min-width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="MyController" class="demo">
<div ng-repeat="eachItem in myArr" class="row">

<span>{{eachItem.name}} => </span>

<label> a
<input name="status-$index" type="radio" ng-model="eachItem.status" value="a">
</label>

<label> b
<input name="status-$index" type="radio" ng-model="eachItem.status" value="b">
</label>

<label> c
<input name="status-$index" type="radio" ng-model="eachItem.status" value="c">
</label>

</div>
</div>






angularjs angularjs-ng-repeat radio-group






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 17:33

























asked Nov 19 at 17:15









Manoj Shrestha

1,39411832




1,39411832








  • 1




    Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
    – Mickers
    Nov 19 at 17:31










  • Awesome. That worked. Thanks @Mickers.
    – Manoj Shrestha
    Nov 19 at 17:37














  • 1




    Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
    – Mickers
    Nov 19 at 17:31










  • Awesome. That worked. Thanks @Mickers.
    – Manoj Shrestha
    Nov 19 at 17:37








1




1




Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31




Try <input name="status-{{ $index }}" type="radio" ng-model="eachItem.status" value="a">
– Mickers
Nov 19 at 17:31












Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37




Awesome. That worked. Thanks @Mickers.
– Manoj Shrestha
Nov 19 at 17:37












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Your code looks fine except the place where you are setting the name attribute to input.



$index in template will be evaluated only if you enclose it inside curly brackets.
EX: {{$index}} else you will get $index in the name. You should use:



 <div ng-repeat="eachItem in myArr track by $index" class="row">
<input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
</div>





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379646%2fangularjs-radio-button-groups-with-name-attribute-not-initializing-properly%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Your code looks fine except the place where you are setting the name attribute to input.



    $index in template will be evaluated only if you enclose it inside curly brackets.
    EX: {{$index}} else you will get $index in the name. You should use:



     <div ng-repeat="eachItem in myArr track by $index" class="row">
    <input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
    </div>





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Your code looks fine except the place where you are setting the name attribute to input.



      $index in template will be evaluated only if you enclose it inside curly brackets.
      EX: {{$index}} else you will get $index in the name. You should use:



       <div ng-repeat="eachItem in myArr track by $index" class="row">
      <input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
      </div>





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Your code looks fine except the place where you are setting the name attribute to input.



        $index in template will be evaluated only if you enclose it inside curly brackets.
        EX: {{$index}} else you will get $index in the name. You should use:



         <div ng-repeat="eachItem in myArr track by $index" class="row">
        <input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
        </div>





        share|improve this answer












        Your code looks fine except the place where you are setting the name attribute to input.



        $index in template will be evaluated only if you enclose it inside curly brackets.
        EX: {{$index}} else you will get $index in the name. You should use:



         <div ng-repeat="eachItem in myArr track by $index" class="row">
        <input name="status-{{$index}}" type="radio" ng-model="eachItem.status" value="a">...
        </div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 21:28









        nkuma_12

        53712




        53712






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379646%2fangularjs-radio-button-groups-with-name-attribute-not-initializing-properly%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno