Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Why does angular and the checkbox model not bind to the checked="checked". The checkbox is unset once angular loads. eg: http://jsfiddle.net/5CZ74/2/

<div ng-app>
   <form>
       <input type="checkbox" ng-model="redirect" checked="checked">
       <input type="text" ng-disabled="redirect==false">
   </form>
</div>

When my form loads I am setting it enabled or disabled on the server as per server entity. How do I get the angular model to correctly bind to this value to enable or disable the input text field.

Thanks.

share|improve this question

1 Answer 1

You have not defined redirect in your model anywhere, so it is undefined, and thus falsy. This is why the the checkbox is not checked. If you want it checked you need to add redirect to your model and set it to true. You need to have a controller and do this:

$scope.redirect = true;

See http://jsfiddle.net/5CZ74/3/

share|improve this answer
1  
So the issue is that the model is still not correctly initialized from the server. With your solution the checkbox is always checked. I want it to be checked if checked="checked and unchecked if that does not exist. –  user1588232 Sep 26 '13 at 19:53
    
The point of angular is that your view (the checkbox) is driven from the model ($scope.redirect). Set the value of $scope.redirect based on whether or not you want the checkbox to be checked. If the value is true, the checkbox will be checked. If the value is false, the checkbox will be unchecked. You can even change what values trigger it being checked and unchecked. See docs.angularjs.org/api/ng.directive:input.checkbox –  dnc253 Sep 26 '13 at 20:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.