vendredi 29 juillet 2016

ngModel remove the Checked status of checkbox automatically

I am using Angular to validate a checkbox and using ng-model for that:

<input type="checkbox" name="news1" value="news1" ng-model="news" <c:if test="${xxxx == yes'}">checked="checked"></c:if>>
<label ng-click="news1();"></label>

When the page where the checkbox is being embedded is called via a link which passes the status of the news1 (yes).. I mean if the user is laready registered for news1 or not. If it's so, the checkbox should be checked dynamically otherwise it's unchecked.

The problem is that when I use/add: ng-model="news1" as attribute in the checkbox, it doesn't work correctly. The checkbox is checked for seconds and it become unchecked suddenly.

In the controller I'm having the following function to change the status of the checkbox after the page has been loaded, so that the user can check and/or uncheck the checkbox:

$scope.news1 = function (){
    if(!$scope.news1){
        $scope.news1 = !$scope.news1;
    } else{
        $scope.news1 = !$scope.news1;
    }
};

The question, how can fix this so that the Checkbox keeps being checked when the status for news1 in the link is: yes. How can I prevent angular from setting the checkbox to unchecked automatically?

Thanks!




Aucun commentaire:

Enregistrer un commentaire