2014-10-28 31 views
0

我正在使用ng-repeat顯示角度列表視圖。我能夠顯示列表。實際上,每行有一個單選按鈕,並且我在屏幕上有一個按鈕。我想要獲取所選單選按鈕的對象按鈕點擊。換句話說,如果我選擇第一行單選按鈕,那麼如果我點擊按鈕,我需要得到它的對象,我將如何實現這一點?如何獲得按鈕點擊角度的對象值?

這裏是我的代碼

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

更新

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> 
+0

您需要$指數傳遞給你的點擊功能。 – user2808895 2014-10-28 05:56:40

+0

在哪裏通過?按鈕是沒有屏幕和每行單選按鈕。一次只選擇一個單選按鈕 – user944513 2014-10-28 05:57:58

回答

1

添加ng-change指令,

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

在控制器

$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 
} 
+0

它第一次工作...等待我會告訴你發生了什麼 – user944513 2014-10-28 06:01:40

+0

請檢查更新..我會應用你的anser – user944513 2014-10-28 06:04:03

+0

請檢查我的更新回答:) – 2014-10-28 06:05:17

1

當您單擊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> 

這是必需的,因爲ng-repeat會創建它自己的範圍。

Working Example

+0

@ user944513:這有幫助嗎? – V31 2014-11-04 06:58:47

0

你只需要使用

ng-value="item" 

,你可以讓你從那裏想要的任何信息。

這裏有一個plunker