javascript - 如何禁用 View 中特定状态的输入。 Angular

标签 javascript html angularjs angular-ui-router

我在应用程序的两个不同位置使用了一个指令,并且我需要在应用程序的一种状态下禁用该指令模板中的输入,而不是另一种状态。

我已经尝试过:

<input type="number" ng-model="data" ng-disabled='$state.current.name == "findRates"'/>

没有运气。如果我将 ng-disable 更改为如下所示:

ng-disabled='checkState()'

使用 Controller 中的 checkState 函数:

$scope.checkState = function() {
  if ($state.current.name == 'findRates') {
    return true;
  }
};

这工作得很好,但是,我想仅在 View 中执行此操作,而不需要 Controller 中的额外位。我只是错过了什么吗?我也尝试过使用 $location,但没有成功,我宁愿使用 $state,但我愿意接受任何建议。提前谢谢您。

最佳答案

因为您的 Controller 将 $state 作为依赖项,但 View 仅暴露于 $scope 变量,因此将变量放在其上,例如

$scope.shouldDisable = $state.current.name == 'findRates'

然后在 View 上使用shouldDisable

<input type="number" ng-model="data" ng-disabled="shouldDisable"/>

关于javascript - 如何禁用 View 中特定状态的输入。 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31438730/

相关文章:

angularjs - Angular JS 未知提供商

javascript - JavaScript 代码中的安全性

javascript - 提交之间不能清空数组

html - 如何判断是什么导致 Internet Explorer 发出安全警告

javascript - 模式转化为数组

javascript - 单击外部时隐藏 Angular UI Bootstrap 弹出窗口

javascript - 无法理解如何在 WordPress 中正确排列 jQuery

javascript - 是否可以使用 jQuery 访问 div 的所有子项

javascript - jQuery .hide() 不在动态创建的元素上运行

javascript - 了解 AngularJS 中的域和上下文根 url 路径