简体   繁体   中英

Align radio buttons vertically in Angular JS

I'm building a form of radio question types.

Here is code for view:

<div class="form-group" ng-class="{ 'has-error': form.$submitted && form[field.id].$invalid }" ng-if="field.type === 'radio'">
   <label for="{{field.id}}">{{field.title}}</label>
   <br>
   <label ng-repeat="value in field.values">
      <input type="radio" id="{{field.id}}" name="field.id" ng-model="formData[field.id]" value="{{value.title}}"> {{value.title}}</label>
   <p class="form-group-note" ng-if="field.info" ng-bind="field.info"></p>

   <div ng-show="form.$submitted" ng-cloack>
      <span class="help-block" ng-show="form['{{field.id}}'].$error.required" ng-if="field.validations.required">Please enter a value, this field is required</span>
   </div>

   Selected Value is : {{formData[field.id]}}
</div>

JSON data I'm feeding is

{
    "groups": [
        {
            "id": "4_2",
            "title": "Passport",
            "sections": [
                {
                    "id": "4_2_section",
                    "fields": [
                        {
                            "id": "select_id",
                                                        "title": "Select type of question",
                            "type": "select",
                            "info": "Always select \"Yes\"",
                            "size": {
                                "width": 100,
                                "height": 1
                            },
                            "validations": {
                                "required": true
                            },
                            "values": [
                                {
                                    "id": 0,
                                    "title": "Not Selected"
                                },
                                {
                                    "id": 1,
                                    "title": "Yes"
                                },
                                {
                                    "id": 2,
                                    "title": "No"
                                }
                            ]
                        }
                    ]

The result I get for the radio questions is like following: 在此处输入图片说明 As you can see, all radio buttons are aligned horizontally. How do I align them vertically?? I mean one radio button on one row.

Add a style to your HTML

.display-block {
 display: block;
}

<label class="display-block" ng-repeat="value in field.values">
    <input type="radio" id="{{field.id}}" name="field.id" ng-model="formData[field.id]" value="{{value.title}}"> {{value.title}}
</label>

Watch what you want to repeat.

<ul>
<li ng-repeat="value in field.values">
   <label for="{{field.id}}"> {{value.title}}</label>
   <input type="radio" id="{{field.id}}" name="field.id" ng-model="formData[field.id]" value="{{value.title}}">
</li>
</ul>

Wrap your inputs in ul, li. Below code will help for sure :

HTML:

<form name="myForm" ng-controller="MyCtrl">
    <p>Favorite Beatle</p>
    <ul>
        <li ng-repeat="person in people">
            <label>{{person.name}}
                <input type="radio" ng-model="$parent.name" name="name" value="{{person.name}}" required />
            </label>
        </li>
    </ul>
    <p><tt>myForm.$invalid: {{myForm.$invalid}}</tt></p>
    <button ng-disabled="myForm.$invalid">Submit</button>
</form>

JavaScript :

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

JsFiddle : https://jsfiddle.net/nikdtu/zp2131zw/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM