2

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.

1 Answer 1

6

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/

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.