I have a form with a checkbox that binds to ng-model property. When I check the checkbox the value is set in the model but when its unchecked, the key and value are never set at all so it fails api validation.
<div class="form-group">
<label for="title">Title</label>
<input type="text" ng-model="post.title" placeholder="Post title..." />
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" ng-model="post.author" placeholder="Your name" />
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea ng-model="post.content"></textarea>
</div>
<div class="form-group">
<label for="visible">Visible?</label>
<input type="checkbox" ng-model="post.is_visible" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Save" />
</div>
blogModule.controller('PostCreateController', ['$scope', '$stateParams', 'PostResource',
function ($scope, $stateParams, PostResource) {
$scope.post = new PostResource();
$scope.addPost = function () {
console.log($scope.post); // post.is_visible is undefined
//$scope.post.$save();
}
}
]);
This is what the model looks like before it's saved:
{
$resolved: true
author: "asdag"
content: "adfadsf"
title: "adgadf"
proto: ...
}
Why is post.is_visible undefined instead of being set to false? What can I do to make it set false?