2016-11-30 126 views
1

有一個問題,在這裏的情況:
我想取消選中第二個複選框時,第一個複選框。

比方說,默認情況下這兩個複選框都被選中。當我點擊第二個複選框時,我希望第一個複選框也取消選中。AngularJs:無法取消複選框時,取消其他複選框

HTML:

<ion-checkbox ng-model="firstCheckbox">First Checkbox</ion-checkbox> 
<ion-checkbox ng-model="secondCheckbox" ng-change="uncheckFirstCheckbox(secondCheckbox)">Second Checkbox</ion-checkbox> 


JS:

$scope.uncheckFirstCheckbox = function(secondCheckbox) { 
     if(secondCheckbox.checked === false) { 
      $scope.firstCheckbox = false; 
     }; 
    }; 


$scope.firstCheckbox把假的,但它在HTML保持取消選中。
我可以知道問題出在哪裏?

+1

嘗試'如果($ scope.secondCheckbox!){$ scope.firstCheckbox = FALSE; };',不要傳遞$ scope在函數調用中的值作爲一個好習慣 – harishr

+0

謝謝你的提示:) @entre –

回答

2

您正在嘗試訪問secondCheckbox.checked屬性,但secondCheckbox已經是false。您應該只檢查secondCheckbox以獲得false的值。代替該條件使用if(!secondCheckbox)

Here是你的代碼工作的小提琴。

HTML

<ion-checkbox ng-model="firstCheckbox">First Checkbox</ion-checkbox> 
<ion-checkbox ng-model="secondCheckbox" ng-change="uncheckFirstCheckbox(secondCheckbox)">Second Checkbox</ion-checkbox> 

JS

$scope.uncheckFirstCheckbox = function(secondCheckbox) { 
    if (!secondCheckbox) { 
     $scope.firstCheckbox = false; 
    }; 
}; 
+0

感謝您的回答! @Divyanshu –