Angularjs复选框在开始时未选中

时间:2016-08-24 05:49:46

标签: angularjs checkbox

我在检查angularjs复选框时遇到问题。

篮子[0] .Partdelivery来自frome db with 1 update()向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>

我还在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()">

在控制器中:

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

但未选中复选框...

1 个答案:

答案 0 :(得分:1)

ng-model是一个双绑定变量。您在第一个示例中传递了1,这不是变量。

试试这个

的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;
});

这是一个plnkr:https://plnkr.co/edit/TywBTouaKuU8xHFNlQQU?p=preview

您还可以查看ngChecked指令https://docs.angularjs.org/api/ng/directive/ngChecked