2017-04-14 483 views
1

在多个复选框的列表中,我有一个“其他”复选框,用户可以在其中插入一些文本。这被定义为:选择md-checkbox内的输入元素将切换复选框

<md-checkbox class="md-checkbox-interactive" ng-model="$ctrl.someVar"> 
    <input type="text" placeholder="Placeholder" ng-model="$ctrl.someVarText"> 
</md-checkbox> 

md-checkbox-interactive用于允许input元件上的指针事件:

md-checkbox.md-checkbox-interactive { 
    .md-label { 
    input { 
     pointer-events: all; 
    } 
    } 
} 

这是从下面的答案采取关于单选按钮类似的问题:https://stackoverflow.com/a/33304119/2051151

但是,无论何时我选择输入元素,该复选框都会被切换。我在这里错过了什么?

回答

0

后有点搜索,我发现我需要添加input元以下的:

ng-click="$ctrl.onClickInput($event)" 

其中函数定义为:

this.onClickInput = function (event) 
{ 
    // Prevent the click event to propagate 
    event.stopPropagation(); 

    // But toggle the checkbox if necessary 
    if (!vm.someVar) vm.someVar = true; 
} 

这将防止不必要的切换的复选框。