我在 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
绑定(bind):
$("#listOptionFruit").kendoDropDownList({
autoBind: true,
filter: 'contains',
dataSource: $scope.listOptionFruitList,
select: function (ev) {
$scope.$apply(function() {
$scope.listOptionFruitSelect(ev);
});
}
});
AngularJS 通过提供自己的事件处理循环来修改正常的 JavaScript 流程。这将 JavaScript 分为经典执行上下文和 AngularJS 执行上下文。只有在 AngularJS 执行上下文中应用的操作才会受益于 AngularJS 数据绑定(bind)、异常处理、属性监视等。
欲了解更多信息,请参阅
关于javascript - 如何使用 AngularJS 禁用基于下拉选择值的输入文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59185707/