Html 如何获取单元格值

Html 如何获取单元格值,html,angularjs,function,html-table,each,Html,Angularjs,Function,Html Table,Each,如何使用ng click指令从HTML表格中获取单元格值? 我需要实现使用ng click捕获行值的最佳方法 Html表格: <div ng-controller="myController"> <table> <thead> <th>Id</th> <th>Name</th> <th>Age&

如何使用ng click指令从HTML表格中获取单元格值?
我需要实现使用ng click捕获行值的最佳方法

Html表格:

<div ng-controller="myController">      
    <table>
        <thead>
            <th>Id</th>
            <th>Name</th>
            <th>Age</th>
            <th>Action</th>
        </thead>
        <tbody>
             <tr ng-repeat="item in stutents">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td><button type="button" ng-click="getCellValue()">View</button></td>
            </tr>
        </tbody>
    </table>

</div>

将对象作为参数传递给函数并访问它

<td><button type="button" ng-click="getCellValue(item)">View</button></td>
var-app=angular.module('firstApp',[]);
app.controller('myController',函数($scope){
$scope.students=[{
名称:'dat',
id:'00',
年龄:12
}];
//数组值…等等
$scope.getCellValue=函数(项){
console.log(item.name);
console.log(item.id);
}
});

身份证件
名称
年龄
行动
{{item.id}
{{item.name}
{{item.age}
{{item.id}

将对象作为参数传递给函数并访问它

<td><button type="button" ng-click="getCellValue(item)">View</button></td>
var-app=angular.module('firstApp',[]);
app.controller('myController',函数($scope){
$scope.students=[{
名称:'dat',
id:'00',
年龄:12
}];
//数组值…等等
$scope.getCellValue=函数(项){
console.log(item.name);
console.log(item.id);
}
});

身份证件
名称
年龄
行动
{{item.id}
{{item.name}
{{item.age}
{{item.id}

使用
$index

<td><button type="button" ng-click="getCellValue($index)">View</button></td>

您还可以从@sachila answer:)获得它。

使用
$index

<td><button type="button" ng-click="getCellValue($index)">View</button></td>
您也可以从@sachila-answer:)获得它。

var-app=angular.module('firstApp',[]);
app.controller('myController',函数($scope){
$scope.students=[{
名称:‘A’,
id:'1',
年龄:12
},{
名称:‘B’,
id:'2',
年龄:10
},
{
名称:‘C’,
id:'3',
年龄:22
}];
//数组值…等等
$scope.getCellValue=函数(索引){
console.log($scope.students[index].id);
}
});

身份证件
名称
年龄
行动
{{item.id}
{{item.name}
{{item.age}
{{item.id}
var-app=angular.module('firstApp',[]);
app.controller('myController',函数($scope){
$scope.students=[{
名称:‘A’,
id:'1',
年龄:12
},{
名称:‘B’,
id:'2',
年龄:10
},
{
名称:‘C’,
id:'3',
年龄:22
}];
//数组值…等等
$scope.getCellValue=函数(索引){
console.log($scope.students[index].id);
}
});

身份证件
名称
年龄
行动
{{item.id}
{{item.name}
{{item.age}
{{item.id}

如果将
过滤器应用到
ng repeat
@SlavaUtesinov,则它将不起作用。它应该会起作用。如果应用了筛选器,则使用
$parent.$index
$child.$index
。现在,这一次是OP问题的另一种方法,在将
过滤器
应用到
ng repeat
@SlavaUtesinov的情况下,它将不起作用。它应该会起作用。如果应用了筛选器,则使用
$parent.$index
$child.$index
。这一次,这是解决OP问题的另一种方法
$scope.getCellValue = function(index) {
   console.log($scope.stutents[index].name);
   console.log($scope.stutents[index].id);
}