以角度获取数据属性

时间:2016-04-21 08:47:13

标签: javascript jquery angularjs

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

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

在我的角度控制器中,我有:

$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)返回undefined。

1 个答案:

答案 0 :(得分:4)

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

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