角度范围变量在锚标记内不可见

angular scope variable is not visible inside anchor tag

本文关键字:范围 变量      更新时间:2023-09-26

您好,我是 Angular 的新手,并尝试通过以下方式调用带有超链接的控制器方法:

<a href="" ng-click="getAssetDetail(contractNum)"> {{item.contractnum}} </a>

其中 item.contractnum 是一个范围变量,当我从"a"标签中拉出它时它是可见的,但在标签内它不可见,我也想把它传回控制器方法 getAssetDetail,但无法弄清楚如何完成它。请指教。

编辑:视图:

<tbody>
        <tr ng-repeat="item in pagedItems[currentPage] ">
            <td><a href="" ng-click="getAssetDetail(item.contractnum)">contract number {{item.contractnum}} </a> <br /> Serial Number:{{item.serialNum}} </td>
        </tr>
    </tbody>

控制器:

 $scope.getAssetDetail = function (contractNum) {
        //My Code Here
    }
 pagedItems=[{"contractnum":"123", "serialNum":"ABC1" },
             {"contractnum":"121", "serialNum":"ABC2" },
             {"contractnum":"124", "serialNum":"ABC3" },
             {"contractnum":"125", "serialNum":"ABC4" } 
            ]

如果你真的有pagedItems喜欢

pagedItems=[{"contractnum":"123", "serialNum":"ABC1" },
         {"contractnum":"121", "serialNum":"ABC2" },
         {"contractnum":"124", "serialNum":"ABC3" },
         {"contractnum":"125", "serialNum":"ABC4" } 
        ]

所以你错误地使用了ngRepeat,因为传递给它简单的对象,如pagedItems[0]

{"contractnum":"123", "serialNum":"ABC1" }

所以在这种情况下的项目123的,ABC1真的没有财产contractnum和其他

对于解决,您可以使用

ng-repeat="item in pagedItems"

或保存在pagedItems对象数组中,如 [[{},...],[{},...],[{},...]]

或使用 limitTofilterpagedItems中选择仅需要的元素

如果您的 pagedItems 数据仅用于一页数据,则需要将 ng-repeat 行更改为:

<tr ng-repeat="item in pagedItems">

如果您的 pagedItems 数据是每个页面的所有数据,则需要将 pagedItems 数据结构更改为具有每个页码属性的对象,如下所示:

pageItems = {
    "1": [{"contractnum": "121"}, ... etc],
    "2": [{"contractnum": "131"}, ... etc]
    ... etc ...
}  
或者作为数组数组

,其中外部数组中的索引是页码(或者可能是页码 - 1),内部数组是每个页面的对象数组,如下所示:

pageItems = [
    [{"contractnum": "121"}, ... etc],
    [{"contractnum": "131"}, ... etc]
]

然后,您现有的ng重复线应该可以工作。