javascript Angularjs 在 ng-repeat 中按下复选框时禁用下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18095171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:43:34  来源:igfitidea点击:

Angularjs Make drop down disabled when pressing checkbox inside ng-repeat

javascriptangularjsangularjs-ng-repeatng-show

提问by Scription

  • I'm trying to display on screen html controls using Meta-Data stored in json file.
  • Also I'm trying to create a state where pressing check box that will make a drop down disabled.
    • I was managed to achieve this for a select box with staticoptions and a select box with dynamicoptions (using ng-options).
    • I could not achieve this when wrapping it with ng-repeat.
  • 我正在尝试使用存储在 json 文件中的元数据在屏幕 html 控件上显示。
  • 此外,我正在尝试创建一种状态,在该状态下,按下复选框将使下拉菜单被禁用。
    • 我设法为带有静态选项的选择框和带有动态选项的选择框(使用 ng-options)实现了这一点。
    • ng-repeat包装时我无法实现这一点。

I have spent a lot of time making this work but in vane.

我花了很多时间来完成这项工作,但在风向标上。

I would appreciate if someone could pin point me to the right direction.

如果有人能指出我正确的方向,我将不胜感激。

I have created a sample code which can be found in Plunkerhere

我创建了一个示例代码,可以在此处的Plunker 中找到

采纳答案by zs2020

From ngRepeatdoc:

来自ngRepeat文档:

The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope

ngRepeat 指令为集合中的每个项目实例化一个模板一次。每个模板实例都有自己的作用域

The trick is to add controlCheckedto formand remove the ng-init. So they will be in the same scope.

诀窍是添加controlCheckedform并删除ng-init。所以它们将在同一范围内。

<div ng-show="control.type == 'Checkbox'">
    <input type="checkbox" ng-model="form.controlChecked" name="{{control.id}}"/>
</div>

<div ng-show="control.type == 'DropdownList'">
     <select    ng-model="control.id" 
                ng-disabled="!form.controlChecked"
                ng-options="value.code as value.name for value in control.items" 
                name="{{control.id}}"
                >
    </select>

Demo

Demo

回答by akonsu

from http://docs.angularjs.org/api/ng.directive:ngRepeat:

来自http://docs.angularjs.org/api/ng.directive:ngRepeat

The ngRepeatdirective instantiates a template once per item from a collection. Each template instance gets its own scope.

ngRepeat指令为集合中的每个项目实例化一个模板一次。每个模板实例都有自己的作用域。

so it looks like you should use the $parentscope: $parent.controlChecked.

所以看起来你应该使用$parentscope: $parent.controlChecked