javascript ng:disabled 不适用于选择 html 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14288436/
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-26 21:14:12  来源:igfitidea点击:

ng:disabled does not work with select html element

javascriptangularjs

提问by Jaroslaw.zawila

I am writing web base project and I am using angularjs. I have find little bug with ng:disabled directive on select html element. If the element is disabled I still could change its value using keyboard arrows down and up. So I would like to achieve is disabled the select element and eliminate this little thing.

我正在编写 Web 基础项目,并且正在使用 angularjs。我在 select html 元素上发现了 ng:disabled 指令的小错误。如果该元素被禁用,我仍然可以使用键盘向下和向上箭头更改其值。所以我想实现的是禁用select元素并消除这个小东西。

Could someone explain how this directive works ?? I have looked at angularjs but cannot figure out it.

有人能解释一下这个指令是如何工作的吗??我看过 angularjs 但无法弄清楚。

Is it a bug or normal behaviour and I cannot understand something??

这是错误还是正常行为,我无法理解某些东西?

Thanks for you answers in advance.

感谢您提前回答。

Example: http://jsfiddle.net/kickwce/m23Gr/1/

示例:http: //jsfiddle.net/kickwce/m23Gr/1/

<select class="select input-large" id="listType" name="listType"
                        ng-model="listType" ng-disabled="listType">
                        <option value="">- choose -</option>
                        <option value="item1">List of tracks</option>
                        <option value="item2">List of collections</option>
                    </select>

回答by Mark Rajcok

The problem appears to be that the select box does not lose focus. A directive can solve that problem:

问题似乎是选择框没有失去焦点。一个指令可以解决这个问题:

myApp.directive('blur', function () {
  return function (scope, element, attrs) {
    attrs.$observe('blur', function (newValue) {
      newValue != "" && element[0].blur();
    });
  }
});

HTML:

HTML:

<select ... ng-model="listType" ng-disabled="listType" blur="{{listType}}">

Fiddle.

小提琴

You should probably be using ng-optionswith your select list.

您可能应该在选择列表中使用ng-options