在angularjs中选择数组中的对象

时间:2013-08-20 19:36:33

标签: angularjs angularjs-ng-click

我试图使其成为可能,以便通过单击以下<td>之一中的“客户端”,我可以从“clients”数组中选择该特定对象并切换到新视图。我假设我想从ng-click开始,只是不确定如何去做。我也不会使用任何jquery。

<div ng-init="clients = [
    {firstname:'Buster', lastname:'Bluth', tagid:'4134'},
    {firstname:'John', lastname:'McClane', tagid:'9845'},
    {firstname:'Mister', lastname:'Spock', tagid:'0905'}
    ]"></div>
<div>
    <div>Clients</div>
        <table>
            <thead>
                <tr>
                     <th>First Name</th>
                     <th>Last Name</th>
                     <th>I-Number</th>
                </tr>
                </thead>
                <tbody>
                   <tr ng-repeat="client in clients">
                       <td>{{client.firstname}}</td>
                       <td>{{client.lastname}}</td>
                       <td>{{client.inumber}}</td>
                   </tr>
                </tbody>
            </table>
        </div>

1 个答案:

答案 0 :(得分:2)

ng-click是正确的方法。您可以像这样获取所选对象

<tr ng-repeat="client in clients" ng-click="redirect(client)">

使用以下方法创建控制器:

function ctrl($scope, $location){
    $scope.redirect = function(client){
        console.log(client);
        $location.url('/someurl'); //this is the routing defined in the $routingProvider, you need to implement it.
    }
}

确保引用包含select 的外部div中的类

<div ng-controller='ctrl'>