0

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 ?

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>
2
  • You need to pass $index to your click function. Commented Oct 28, 2014 at 5:56
  • where to pass ?button is no screen and radio button on each row.only one radio button is selected at one time Commented Oct 28, 2014 at 5:57

3 Answers 3

1

add ng-change directive,

<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
}
Sign up to request clarification or add additional context in comments.

6 Comments

please check that update ..and I will apply your anser
sir actually your edited anser also work only first time .Mean when I select first row it give 0 index.then I select second row it give 1 index (deselect the first row).but again when i select first row again it not give alert mean on change event not fire
Do you have any idea why it is not firing second time
ok I will take static data because I am getting data from local webservice and used plunker
|
1

you can use the $parent notation when you click the radio button inside ng-repeat.

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

Working Example

1 Comment

@user944513: Did this help u?
0

You just need to use

ng-value="item"

and you can get any info you want from there.

Here's a plunker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.