繁体   English   中英

如何使用 AngularJS 基于下拉选择值禁用输入文本框?

[英]How can i disable an input text box based on dropdown selection value using AngularJS?

我在 AngularJS 的 JavaScript 中有以下内容

$("#listOptionFruit").kendoDropDownList({
    autoBind: true,
    filter: 'contains',
    dataSource: $scope.listOptionFruitList,
    select: $scope.listOptionFruitSelect
});

$scope.listOptionFruitSelect = function (e)
{            
   $scope.listOptionFruit = e.dataItem;
   if ($scope.listOptionFruit === 'Strawberry') {
       $scope.enableMe = true;
   } 
   else 
   {
       $scope.enableMe =  false;
   }                               
}

而cshtml端如下:

<label for="listOptionFruit">Fruits</label>
<input id="listOptionFruit" name="listOptionFruit"  required />


<label for="myValue">MyValue</label>
<input type="text"  id="myValue" ng-disabled="enableMe"  />

因此 enableMe 的值是正确的,但文本框似乎没有被禁用或操作似乎没有生效。

因此,如果用户从列表中选择草莓,则文本框将被禁用,否则将被启用

由于事件发生在 AngularJS 框架之外,因此需要与$apply绑定:

$("#listOptionFruit").kendoDropDownList({
    autoBind: true,
    filter: 'contains',
    dataSource: $scope.listOptionFruitList,
    select: function (ev) {
        $scope.$apply(function() {
            $scope.listOptionFruitSelect(ev);
        });
    }
});

AngularJS 通过提供自己的事件处理循环来修改正常的 JavaScript 流程。 这将 JavaScript 分为经典和 AngularJS 执行上下文。 只有在 AngularJS 执行上下文中应用的操作才会受益于 AngularJS 数据绑定、异常处理、属性监视等。

有关更多信息,请参阅

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM