最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS ng-repeat with multiple radio inputs - Stack Overflow

programmeradmin0浏览0评论

I've got small problem with ng-repeat on persons with multiple radio inputs (yes/no). Input names should be different because of name="person.name" but it behaves like they all are the same. Somebody knows how to fix this?

/

HTML:

<form name="myForm" ng-controller="MyCtrl">
    <p>Decisions</p>
    <ul>
        <li ng-repeat="person in people">
            <label>{{person.name}}
                <input type="radio" ng-model="person.decision" name="person.name" value="Yes" />
                <input type="radio" ng-model="person.decision" name="person.name" value="No" />
            </label>
        </li>
    </ul>
</form>

JS:

function MyCtrl($scope) {
    $scope.people = [{
        name: "John"
    }, {
        name: "Paul"
    }, {
        name: "George"
    }, {
        name: "Ringo"
    }];
}

I've got small problem with ng-repeat on persons with multiple radio inputs (yes/no). Input names should be different because of name="person.name" but it behaves like they all are the same. Somebody knows how to fix this?

http://jsfiddle/Chotkos/EbU8g/

HTML:

<form name="myForm" ng-controller="MyCtrl">
    <p>Decisions</p>
    <ul>
        <li ng-repeat="person in people">
            <label>{{person.name}}
                <input type="radio" ng-model="person.decision" name="person.name" value="Yes" />
                <input type="radio" ng-model="person.decision" name="person.name" value="No" />
            </label>
        </li>
    </ul>
</form>

JS:

function MyCtrl($scope) {
    $scope.people = [{
        name: "John"
    }, {
        name: "Paul"
    }, {
        name: "George"
    }, {
        name: "Ringo"
    }];
}
Share Improve this question edited Jul 1, 2022 at 20:09 Jonas 129k103 gold badges328 silver badges405 bronze badges asked Jul 15, 2014 at 16:05 chotkoschotkos 1801 silver badge10 bronze badges 1
  • For more information about when to use curly braces in angular, refer to stackoverflow./questions/17878560/… – Jorge Zuverza Commented Jul 15, 2014 at 16:20
Add a ment  | 

3 Answers 3

Reset to default 6

You need to use {{ person.name }}, rather than "person.name" in that context.

I don't know why you got downvoted. Your question was fine. So you are using the name property which is traditional in an HTML form. Since name isn't part of angular, you need to wrap the person.name in curly brackets name="{{person.name}}" so angular knows to parse it.

Please see here: http://jsfiddle/Chotkos/EbU8g/ that happend because you wrapped all inputs into label tag

  <form name="myForm" ng-controller="MyCtrl">
        <p>Decisions</p>
        <ul>
            <li ng-repeat="person in people">
                <label>{{person.name}}</label>
                <input type="radio" ng-model="person.decision" name="person.name" value="Yes" />
                <input type="radio" ng-model="person.decision" name="person.name" value="No" />
            </li>
        </ul>
    </form>
发布评论

评论列表(0)

  1. 暂无评论