2017-02-24 42 views
0

我使用ng模型和ng-true-value和ng-false-value的複選框。 它工作正常,但是當它的值爲1並且默認情況下它被選中時,它將需要2次點擊才能更改值。Ang ng模型在使用ng true和ng false時需要2次單擊

有什麼方法與第一次點擊做到這一點

代碼示例:

<input type="checkbox" ng-checked="CreateUpdateCat.IsDiscussionAllowed=='1'" ng-model="CreateUpdateCat.IsDiscussionAllowed" ng-true-value="1" ng-false-value="2" > 

控制器代碼:

angular.forEach($scope.CategoryData,function(v,k){ 
         if(v.id == id) 
         { 
$scope.CreateUpdateCat = {IsDiscussionAllowed:v.IsDiscussionAllowed,Name:v.Name}; 
}; 
}); 

這裏是plunkr網址:https://plnkr.co/edit/JApepZfvNrlqDj9vine0?p=preview 值默認爲1但複選框未顯示檢查

回答

2

請使用ng-checkedng-model,否則角度會變得混亂。並有可能會得到不可預測的結果。

不要只用一個指令ng-checkedng-model

+0

但如果值是1,那麼它不是表示檢查默認 –

+0

後控制器代碼也 –

+0

不能明白你可以創建一個小提琴? –

0

NG-檢查不應與NG-模型等可以使用明智的你會得到意想不到的bihavior, NG檢查文檔:https://docs.angularjs.org/api/ng/directive/ngChecked

要麼使用

<input type="checkbox" ng-model="checkboxModel.value" 
     ng-true-value="'YES'" ng-false-value="'NO'"> 

OR

<input type="checkbox" ng-checked="checkboxModel.value" 
     ng-true-value="'YES'" ng-false-value="'NO'"> 
0

儘量把你輸入這樣

<input type="checkbox" ng-model="CreateUpdateCat.IsDiscussionAllowed" ng-true-value="'1'" ng-false-value="'2'"> 

在您plunker,德範圍CreateUpdateCat值是字符串。

0

有些時候angualrjs範圍保持污垢,你可能要使用這樣的:

$timeout(function(){ 
    angular.forEach($scope.CategoryData,function(v,k){ 
    if(v.id == id){ 
     $scope.CreateUpdateCat = {IsDiscussionAllowed:v.IsDiscussionAllowed,Name:v.Name}; 
    }; 
    }); 
}); 

這樣的模型更新自己的DOM元素的valeue