Currently, my Angular code is set up to validate a checkbox using ng-model
:
<input type="checkbox" name="news1" value="news1" ng-model="news" <c:if test="${xxxx == yes'}">checked="checked"></c:if>>
<label ng-click="news1();"></label>
The issue arises when the page containing the checkbox is accessed via a link that passes the status of news1 (yes). This determines whether the user is already signed up for news1 or not. In such cases, the checkbox should be dynamically checked or unchecked based on the status.
However, adding ng-model="news1"
as an attribute to the checkbox does not work as expected. The checkbox appears briefly checked before becoming unchecked again.
In my controller, I have a function designed to change the checkbox's status after the page has loaded:
$scope.news1 = function (){
if(!$scope.news1){
$scope.news1 = !$scope.news1;
} else{
$scope.news1 = !$scope.news1;
}
};
My question is, how can I ensure that the checkbox remains checked when the news1 status in the link is "yes"? And how do I prevent Angular from automatically unchecking the checkbox?
Thank you!