在Angular JS中垂直对齐单选按钮

时间:2016-04-22 02:22:44

标签: javascript html css angularjs

我正在构建一种无线电问题类型。

以下是视图代码:

                <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数据

{
    "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"
                                }
                            ]
                        }
                    ]

我收到的广播问题的结果如下: enter image description here 如您所见,所有单选按钮都水平对齐。 我如何垂直对齐?我的意思是一排上有一个单选按钮。

3 个答案:

答案 0 :(得分:1)

为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>

答案 1 :(得分:0)

注意你想重复的内容。

<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>

答案 2 :(得分:0)

以ul,li包裹您的输入。下面的代码肯定会有所帮助:

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/