2

I have a problem to make an angularjs checkbox checked.

the basket[0].Partdelivery come frome db with 1 update() make a post to the db

<label class="checkbox-inline">
    <input type="checkbox" id="inlineCheckbox1"  class="checkbox" ng-model="basket[0].PartDelivery" ng-true-value="1" ng-false-value="0" ng-change="update()">
    {{basket[0].PartDelivery}}
</label>

<label class="checkbox-inline">
    <input type="checkbox" id="inlineCheckbox1"  class="checkbox" ng-model="1" ng-true-value="1" ng-false-value="0" ng-change="update()">
</label>

And I also tested this in the html:

<div ng-controller="basketController as bc" ng-init="basket={basket}; deliveryaddress={deliveryaddress}; getcheckboxes(basket[0].PartDelivery)">

<input type="checkbox" id="inlineCheckbox1"  class="checkbox" ng-model="partDeliveryCheck.value" ng-true-value="1" ng-false-value="0" ng-change="update()">

in the controller:

$scope.getcheckboxes = function(PartDelivery){
    $scope.partDeliveryCheck={value: PartDelivery};
};

but the checkbox is not checked...

1 Answer 1

1

The ng-model is a double-bind variable. You've passed in a 1 in your first example, which can't be a variable.

Try this

index.html

<input type="checkbox" ng-model="vm.checked">
Is checked: {{vm.checked}}

Controller.js

app.controller('MainCtrl', function() {
  var vm = this;
  vm.checked = true;
});

Here's a plnkr: https://plnkr.co/edit/TywBTouaKuU8xHFNlQQU?p=preview

You can also take a look at the ngChecked directive https://docs.angularjs.org/api/ng/directive/ngChecked

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

1 Comment

@Andreas Please accept the answer if this solved your problem.

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.