gpt4 book ai didi

javascript - 以 Angular 获取数据属性

转载 作者:行者123 更新时间:2023-11-30 09:50:35 25 4
gpt4 key购买 nike

我正在尝试从中获取数据属性:

<button ng-click="EditPlayer(name, position, number, age)" id="btnEdit" class="btn btn-successes" data-playerid="{{player.id}}">Save</button>

在我的 Angular Controller 中我有:

$scope.EditPlayer = function(name, position, number, age) {

var player_id = $(this).data('playerid');

console.log(player_id);

var player = new Player(name, position, number, age);

$http({
method : "PUT",
url : 'api.php/players/'+player_id,
data: JSON.stringify(player),
}).then(function mySucces(player) {
$scope.players.push(player);
}, function myError(response) {
$scope.showError = response.statusText;
});
};

我需要获取该属性,以便我可以针对每个 player_id 进行更新(PUT 请求)。问题是 $(this).data(player_id)返回未定义。

最佳答案

只需将它作为另一个参数传递给函数

<button ng-click="EditPlayer(name, position, number, age, player.id)" id="btnEdit" class="btn btn-successes" data-playerid="{{player.id}}">Save</button>

然后

$scope.EditPlayer = function(name, position, number, age,player_id) {
console.log(player_id)

关于javascript - 以 Angular 获取数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36764367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com