【问题标题】:how to send object from html ng-repeat to js function如何将对象从 html ng-repeat 发送到 js 函数
【发布时间】:2019-11-08 15:54:18
【问题描述】:

我从 db 获取数据并将其放在一个范围内,然后通过 html 中的 ng-repeat 迭代这些数据。 我要做的是在用户选中复选框以获取另一个基于 id 的数据时发送选定的对象。我尝试在复选框上使用 ng-model 和 ng-change 但它只发送 true 或 false 。

HTML

<div id="frame" class="widget-content" style="height:50vh; overflow:auto;">
    <table  style="font-size: 12px;display: inline-block;">
        <thead>
            <tr>
                <th>Check</th>
                <th>Group Code</th>
                <th>Group Name</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="g in Groups | unique : 'Group_code'"
                ng-click="showInEdit(g)"
                ng-class="{selected:g === selectedRow}">
                <td><input type="checkbox" ng-model="g"
                           ng-true-value="g" ng-change="Getrequest2(g)"/>
                <td>{{g.Group_code}}</td>
                <td>{{g.group_name_latin}}</td>**
            </tr>
        </tbody>
    </table>
    <table  style="font-size: 12px; float: left;">
        <thead>
            <tr>
                <th>Check</th>
                <th>Item Code</th>
                <th>Item Name</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="i in items | unique : 'Item_code'"
                ng-click="showInEdit(i)"
                ng-class="{selected:i === selectedRow}">
                <td><input type="checkbox" />
                <td>{{i.Item_code}}</td>
                <td>{{i.ITEM_NAME_LATIN}}</td>
            </tr>
        </tbody>
    </table>
</div>

Angularjs

$scope.Getrequest1 = function (g) {
    //console.log(g);
    $scope.typecd = g.TypeCode;
    $scope.ShowLoading('slow');
    LOASrv.GetGroup("LOAApprovalSetup/GetGroup" +
                    "?typ=" + g.TypeCode +
                    '&hospid=' + $scope.hospitalid + 
                    '&userky=' + $scope.userkey
    ).then(function (response) {
        $scope.Groups = (response.data);
        $scope.HideLoading('slow');
    })
}

$scope.Getrequest2 = function (i) {
    console.log(i);
    $scope.ShowLoading('slow');
    LOASrv.GetItem("LOAApprovalSetup/GetItem" + 
                   "?typ=" + $scope.typecd + 
                   '&hospid=' + $scope.hospitalid + 
                   '&userky=' + $scope.userkey +
                   '&groupcd=' + i
    ).then(function (response) {
        $scope.items = (response.data);
        $scope.HideLoading('slow');
    })
}

【问题讨论】:

  • HTML 有ng-change="Getrequest2(g)",但JS 有$scope.Getrequest2 = function (i)。看起来代码正在将一个对象传递给一个需要字符串的函数。
  • 感谢回复,请问如何解决?接收对象
  • 在复选框的 onchange 事件中,您可以从您的 html 传递 ID 或您需要进行另一个 api 调用的代码。你的 onchange 应该像 ng-change="Getrequest2(g.Group_code)" 。假设 group_code 是您需要在第二个 api 调用中传递的每个对象的代码。

标签: javascript html angularjs


【解决方案1】:

您将“g”对象分配给复选框 ng-model。

这会在复选框选择时将“g”的值从对象更改为布尔值。

这就是你得到真/假的原因

ng-model=“g.someBooleanName”试试

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2016-11-28
    • 2020-07-28
    • 1970-01-01
    • 2018-09-23
    相关资源
    最近更新 更多