复选框未绑定到angularjs中的范围 [英] Checkbox not binding to scope in angularjs
问题描述
我正在尝试使用 ng-model 将复选框绑定到范围.复选框的初始状态对应于范围模型就好了,但是当我选中/取消选中复选框时,模型不会改变.需要注意的是,模板是在运行时使用 ng-include 动态加载的
I am trying to bind a checkbox to scope using ng-model. The checkbox's initial state corresponds to the scope model just fine, but when I check/uncheck the checkbox, the model does not change. Some things to note is that the template is dynamically loaded at runtime using ng-include
app.controller "OrdersController", ($scope, $http, $location, $state, $stateParams, Order) ->
$scope.billing_is_shipping = false
$scope.bind_billing_to_shipping = ->
console.log $scope.billing_is_shipping
<input type="checkbox" ng-model="billing_is_shipping"/>
当我选中该框时,控制台记录为 false,当我取消选中该框时,控制台再次记录为 false.我在范围上也有一个订单模型,如果我将复选框的模型更改为 order.billing_is_shipping,它就可以正常工作
When I check the box the console logs false, when I uncheck the box, the console again logs false. I also have an order model on the scope, and if I change the checkbox's model to be order.billing_is_shipping, it works fine
推荐答案
我在这个问题上挣扎了一段时间.有效的是将输入绑定到对象而不是原始对象.
I struggled with this problem for a while. What worked was to bind the input to an object instead of a primitive.
<!-- Partial -->
<input type="checkbox" ng-model="someObject.someProperty"> Check Me!
// Controller
$scope.someObject.someProperty = false
这篇关于复选框未绑定到angularjs中的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!