小编Sta*_*fen的帖子

通过取消选中带有指令的复选框来更新模型

所以我想要做的是与复选框的依赖.因此,取消选中它所依赖的复选框后,将禁用+取消选中相关复选框.出于某种原因,取消选中指令内的复选框可以完成作业,如禁用和取消选中它,但绑定到它的模型不会更新.

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>
Run Code Online (Sandbox Code Playgroud)

控制器(默认选项):

$scope.test = {
  dependency: true,
  optional: false
}
Run Code Online (Sandbox Code Playgroud)

指示:

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
    }
  })
}
Run Code Online (Sandbox Code Playgroud)

编辑:对,这是插件.

angularjs angularjs-directive

3
推荐指数
1
解决办法
2198
查看次数

标签 统计

angularjs ×1

angularjs-directive ×1