我在应用程序的两个不同位置使用了一个指令,并且我需要在应用程序的一种状态下禁用该指令模板中的输入,而不是另一种状态。
我已经尝试过:
<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/