2017-02-10 14 views
1

JSPAngularjs data-ng-click不會再次工作......怎麼樣?

<form> 
    <table class="countrys" data-name="tableCountry"> 
    <tr bgcolor="lightgrey"> 
    <th>Country ID</th> 
    <th>Country Name</th> 
    </tr> 
    <tr data-ng-repeat="c in allCountrys" 
    data-ng-click="selectedCountry(c, $index);" 
    data-ng-class="getSelectedClass(c);"> 
    <td>{{c.countryId}}</td> 
    <td>{{c.countryName}}</td> 
    </tr> 
    </table> 
</form> 

控制器

$scope.selectedCountry = function(country, index){ 
angular.forEach($scope.allCountrys,function(value, key) { 
     if (value.countryId == country.countryId) { 
     $scope.selectedCountry = country; 
     } 
}); 

$scope.selectedRowCountry = index; 
} 


$scope.getSelectedClass = function(country) { 

if ($scope.selectedCountry.countryId != undefined) { 
    if ($scope.selectedCountry.countryId == country.countryId) { 
    return "selected"; 
    } 
} 
return ""; 
}; 

CSS

tr.selected { 
    background-color: #aaaaaa; 
} 

有我的網頁上這個表,一旦我按1行,它會選擇它,它改變了顏色,它在兩個功能...

但一旦我點擊另一行,我t不會去selectedCountry函數,但只能進入sgetSelectedClass函數

我不知道爲什麼,我只是想要能夠選擇一行,然後另一個等等......所以總是隻有一行是選擇

你能幫助我嗎?

回答

2

您將$scope.selectedCountry定義爲函數,但是在第一次單擊selectedCountry時,通過在ng-click函數內調用$scope.selectedCountry = country;來使$scope.selectedCountry成爲對象。

因此,請保留您的範圍變量。

$scope.selectedCountry = function(country, index){ 
angular.forEach($scope.allCountrys,function(value, key) { 
    if (value.countryId == country.countryId) { 
    $scope.selectedCountry = country; // rename this scope variable 
    } 
}); 
+0

hu,我剛發現。 –

+0

thx男人,你救了我.... – aaaaaaaa1