javascript AngularJS - 禁用 ng 的错误?不更新新值

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

AngularJS - bug with ng-disabled? Not updating with new values

javascripthtmlangularjsvalidation

提问by Daniel Jamrozik

I'm somewhat new to angular and have come across a very weird problem, I'm sure others have too.

我对 angular 有点陌生,遇到了一个非常奇怪的问题,我相信其他人也有。

Let's say that this is the code for my button:

假设这是我的按钮的代码:

<button type="submit" ng-click="submit(something)" id="coolButton" 
        ng-disabled="type === 0 || code === ''" 
        >Check-In</button>

So, basically if either type is 0 or code is nothing (no text), then this button will be disabled.

所以,基本上如果任一类型为 0 或代码什么都没有(没有文本),那么这个按钮将被禁用。

Now's here where my problem starts: If I load the page with type = 0and code = '', then sure enough it's disabled. If, I change both of these values then of course the button will be enabled.

现在是我的问题开始的地方:如果我使用type = 0和加载页面code = '',那么肯定它已被禁用。如果我更改了这两个值,那么当然会启用该按钮。

However, if I change the values back to 0and '', then the button won't become disabled again. I know for a fact that the values are in fact 0 and '' as I've printed their values out on the page.

但是,如果我将值改回0and '',则该按钮将不会再次被禁用。我知道这些值实际上是 0 和 '' 因为我已经在页面上打印了它们的值。

What could be causing ng-disabledto not run the expression again?

什么可能导致ng-disabled不再运行表达式?

采纳答案by Radim K?hler

I created working plunker herefor you, to check that scenario described above is working:

在这里为您创建了工作 plunker,以检查上述场景是否有效:

<div ng-controller="HomeCtrl">

    <button type="submit" 
      ng-click="submit(something)" 
      id="coolButton" 
      ng-disabled="type === 0 || code === ''" 
      >Check-In</button><br />

    <div>type: <input ng-model="type" type="number" /></div>
    <div>code: <input ng-model="code" type="text"   /></div>

    <div>is type equal zero: {{type === 0 }}</div>
    <div>is type code empty: {{code === '' }}</div>

</div>

<script type="text/javascript"> 
var app = angular
.module('MyApp', [])
.controller('HomeCtrl', ['$scope', function ($scope) { 
  $scope.something = function(sth){alert(sth)};
  $scope.type = 0;
  $scope.code = "";
}])
</script>

Important parts here are inside of the controller, where we explicitly init the code and type. Otherwise both will start with undefined/null.

这里的重要部分在控制器内部,我们在那里显式地初始化代码和类型。否则两者都将以 undefined/null 开头。

Check it here

在这里检查

Also, I would strongly suggest to change that style of $scope.type, $scope.code. It could bring some surprising behaviour sooner or later...

另外,我强烈建议更改 $scope.type、$scope.code 的样式。它迟早会带来一些令人惊讶的行为......

We should use some kind of Model cluster, which represents some reference which could be easily passed and does have a dot (see: https://stackoverflow.com/a/21882900/1679310)

我们应该使用某种模型集群,它代表一些可以轻松传递的引用并且确实有一个点(参见:https: //stackoverflow.com/a/21882900/1679310

// controller
$scope.Model = {
  type : 0,
  code : '', 
}

// directive ngDisabled in the view
ng-disabled="Model.type === 0 || Model.code === ''"