所以我想要做的是对复选框的依赖。因此,一旦取消选中它所依赖的复选框,该依赖复选框将被禁用 + 取消选中。出于某种原因,从指令中取消选中复选框可以完成这项工作,例如禁用和取消选中它,但绑定到它的模型不会更新。
HTML:
<div>
<input type="checkbox" data-ng-model="test.dependency"/>
<span>unchecking this one will disable the next</span>
</div>
<div>
<input dependent="test.dependency" type="checkbox" data-ng-model="test.optional" />
<span>this checkboxs directive will uncheck it when the first one is unchecked, but the model doesn't get updated, not it's {{test.optional}}</span>
</div>
控制器(用于默认选项):
$scope.test = {
dependency: true,
optional: false
}
指示:
restrict: 'A',
link: function(scope,elem,attrs){
scope.$watch(attrs.dependent,function(val){
if (!val){
elem[0].checked = false;
elem[0].disabled = true
} else {
elem[0].disabled = false
}
})
}
编辑:对,这是笨拙的。