【问题标题】:How can i disable an input text box based on dropdown selection value using AngularJS?如何使用 AngularJS 根据下拉选择值禁用输入文本框?
【发布时间】:2020-03-29 19:51:54
【问题描述】:

我在 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 的值是正确的,但是文本框似乎没有被禁用或操作似乎没有生效。

所以如果用户从列表中选择草莓,那么文本框被禁用,否则它被启用

【问题讨论】:

    标签: javascript angularjs kendo-ui


    【解决方案1】:

    由于事件发生在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 数据绑定、异常处理、属性监视等。

    有关详细信息,请参阅

    【讨论】:

    • 由 georgeawg 用上述解决方案排序。我使用了错误的变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多