【问题标题】:Access the radio button value created by using ng-repeat访问使用 ng-repeat 创建的单选按钮值
【发布时间】:2016-06-15 17:55:12
【问题描述】:

我有一个表格,表格的第一列是单选按钮。我使用 ng-repeat 创建了。单击时,我必须访问选中的单选按钮值,问题是我无法访问该值。

<div class="panel-heading">
    <h3 class="panel-title mainPanelTitle">Future Appointments</h3>
</div>
<div class="panel-body">
    <div class="table-responsive noSwipe tableContainer" tasty-table bind-resource="futureAppointmentsMap" bind-filters="filters">
        <div class="col-sm-3 tableSearch">
            <input type="text" class="form-control input-sm"
            placeholder="Search" ng-model="filters" value=""/>
        </div>
        <table id="tblPatientMyAppointmentsFuture" class="table table-bordered">
            <thead tasty-thead bind-not-sort-by="notSortBy"></thead>
            <tbody>
                <tr ng-repeat="appointment in rows">
                    <td>
                        <label class="radio-inline">
                            <input type="radio" name="rbnTagAppointment" value="{{appointment.id}}" ng-model="selectedAppointmentId">
                        </label>
                    </td>
                    <td ng-class="{{appointment.timeLineId!=0?'tdImage':''}}" data-title="No">
                        {{$index+1}} 
                        <img ng-if="appointment.timeLineId!=0" ng-src="img/brightfuture_logomedium.png"/>
                    </td>
                    <td data-title="Time">
                        {{changeDateFormat(appointment.date)}}<br>
                        {{appointment.time}}
                    </td>
                    <td data-title="Specialty">
                        {{appointment.specialtyId=="5"?'Pediatrics':appointment.specialty}}
                    </td>
                    <td data-title="Physician">
                        {{appointment.physicianName}}<br/>
                        {{appointment.orgName}} 
                    </td>
                </tr>
            </tbody>
        </table>
        <div tasty-pagination ng-show="rows.length>0"></div>
        <div class="divNoData" ng-show="rows.length==0">
            No data available
        </div>
        <div class="text-center">
            <button class="btn btn-default btn-sm" ng-click="tabAppointmentWithImmunization()">Upload</button>
        </div>
    </div>
</div>

在我的控制器中。

 $scope.tabAppointmentWithImmunization = function() {
    console.info($scope.selectedAppointmentId);
};

当我打印它显示未定义的值时。

【问题讨论】:

  • 我修复了你的 html,你有很多不需要的结束标签(为什么?),你可能想复制它。
  • @MartijnWelker 抱歉,这是复制粘贴问题...

标签: jquery angularjs radio-button


【解决方案1】:

去掉花括号,不需要插值:

ng-value="appointment.id"

我还将您的value 属性更改为ng-value - 与ng-repeat 共同使用。

更多详情:https://docs.angularjs.org/api/ng/directive/ngValue

【讨论】:

    【解决方案2】:

    如果要绑定 $scope 值,请使用 ng-value,如果要应用双向绑定,则 ng-value 和 ng-model 应该相同。

    &lt;input type="radio" name="rbnTagAppointment" ng-value="selectedAppointmentId" ng-model="selectedAppointmentId"&gt;

    【讨论】:

    • 我不需要任何文本来显示...我只想访问选定的单选按钮值....我将值更改为 ng-value 但它不起作用....
    • 你能分享我吗,行中的数据是什么?
    • 没关系..问题是当将值分配给 $scope.selectedAppointmentId 时,它会选择相应的单选按钮,但是当更改选择时模型不会更新。
    • 您在 ng-repeat 中使用 selectedAppointmentId ,恐怕这对每个都是相同的值。每一行的 selectedAppointmentId 中应该有一个唯一值。
    猜你喜欢
    • 1970-01-01
    • 2015-06-27
    • 2015-09-22
    • 2022-11-11
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多