我在将 bool 值传递给文本时遇到了一个奇怪的问题。这是jsfiddle
<div ng-app ng-controller="MainCtrl">
<p>First Name:
<input ng-model="first" ng-readonly="true"/>
</p>
<p>Last Name:
<input ng-model="second" ng-readonly="{{truefalse}}"/>
</p>
</div>
function MainCtrl($scope) {
$scope.first = "Angular";
$scope.second = "JS";
$scope.truefalse = "true";
}
有人可以解释一下为什么第二个字段仍然可以修改吗?
最佳答案
您需要在不带大括号的情况下以 ng-readonly 传递范围。 并且 $scope.true false 不应该是字符串,因此不需要引号。
<div ng-app ng-controller="MainCtrl">
<p>First Name:
<input ng-model="first" ng-readonly="true"/>
</p>
<p>Last Name:
<input ng-model="second" ng-readonly="truefalse"/>
</p>
</div>
function MainCtrl($scope) {
$scope.first = "Angular";
$scope.second = "JS";
$scope.truefalse = true;
}
关于javascript - AngularJS:使用 ng-readonly 只读输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20903649/