简体   繁体   English

如何在角度单击按钮时获取对象值?

[英]how to get object value on button click in angular?

I am displaying a list view in angular using ng-repeat .I an able to display list .Actually there is one radio button in each row .And I have one button on screen .I want to get object of selected radio button on button click .In other words If I select first row radio button then if I click button I need get it object how I will achieve this ? 我正在使用ng-repeat以角度显示列表视图。我能够显示列表。实际上每行有一个单选按钮。并且我在屏幕上有一个按钮。我想在按钮单击时获取所选单选按钮的对象换句话说。如果我选​​择第一行单选按钮,那么如果我单击按钮,我需要使它成为对象,我将如何实现这一目标?

here is my code 这是我的代码

<table>

    <tr id="$index"ng-repeat= "a in data " ng-click="getselectedRow(a)">
        <td> {{a.condidateID}} </td>
        <td> {{a.condidateName}} </td>
        <td> {{a.territory}} </td>
        <td> {{a.voteCount}} </td>
        <td>  <input type="radio" name="chickenEgg" value="egg" ng-model="chekratiovalue"></td>

    </tr>
</table>
        <button style="left: 40%;top: 40%;float: left;position: relative" ng-click="voteForPerson()">Vote</button>

Update 更新资料

 function userdetail($scope,$http,$location,$routeParams){
        console.log(JSON.parse($routeParams.userDetail));
        var userDetail=JSON.parse($routeParams.userDetail);
        $scope.data=userDetail.data;
        console.log($scope.data);
        console.log($scope.data);
        $scope.changeEvnt = function(index) {
            $scope.activeRow = index;
            alert( $scope.activeRow);
        }

        $scope.voteForPerson = function() {
            var selcted = $scope.data[activeRow ];
        }
    }


<table>

    <tr id="$index"ng-repeat= "a in data " ng-click="getselectedRow(a)">
        <td> {{a.condidateID}} </td>
        <td> {{a.condidateName}} </td>
        <td> {{a.territory}} </td>
        <td> {{a.voteCount}} </td>
        <td>  <input type="radio" name="chickenEgg" value="egg" ng-model="chekratiovalue" ng-change="changeEvnt($index)"></td>

    </tr>
</table>
        <button style="left: 40%;top: 40%;float: left;position: relative" ng-click="voteForPerson()">Vote</button>

add ng-change directive, 添加ng-change指令,

<input type="radio" name="chickenEgg" value="egg" ng-model="chekratiovalue" ng-change="chengeEvnt($index)">

In controller 在控制器中

$scope.chengeEvnt = function(index) {
    $scope.activeRow = index;     // get the index when changing the radio button
}

$scope.voteForPerson = function() {
     var selected = $scope.data[$scope.activeRow];   // get the row of selected radio button using the `$scope.activeRow` function
}

you can use the $parent notation when you click the radio button inside ng-repeat. 单击ng-repeat内的单选按钮时,可以使用$ parent表示法。

 <table>

    <tr id="$index" ng-repeat= "a in data ">
        <td> {{a.condidateID}} </td>
        <td> {{a.condidateName}} </td>
        <td> {{a.territory}} </td>
        <td> {{a.voteCount}} </td>
        <td>  <input type="radio" name="chickenEgg" value="{{a.chekratiovalue}}" ng-model="$parent.chekratiovalue"></td>

    </tr>
</table>

This is required as ng-repeat creates its own scope. 这是必需的,因为ng-repeat创建了自己的作用域。

Working Example 工作实例

You just need to use 您只需要使用

ng-value="item"

and you can get any info you want from there. 您可以从那里获取任何想要的信息。

Here's a plunker 这是一个小矮人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM