ng-disable未正确呈现

时间:2017-07-06 05:19:04

标签: javascript angularjs

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
 $scope.firstName =false;
    $scope.toggle = function(){
        $scope.firstName =!$scope.firstName ;
    };
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl">
      <button ng-click=toggle()>ToGgLe</button>
      <input type="text" ng-disabled="{{firstName}}">
   </div>
  </body>
</html>

这里我试图用代码中显示的函数切换输入字段。我可以看到DOM中的值发生变化,但它没有正确地呈现角度...输入不是切换...帮帮我... ...提前致谢

2 个答案:

答案 0 :(得分:3)

您不需要进行字符串插值,因为ngDisabled接受expression直接firstName传递。

<input type="text" ng-disabled="firstName">

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
 $scope.firstName =false;
    $scope.toggle = function(){
        $scope.firstName =!$scope.firstName ;
    };
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl">
      <button ng-click=toggle()>ToGgLe</button>
      <input type="text" ng-disabled="firstName">
   </div>
  </body>
</html>

答案 1 :(得分:2)

您不必使用插值运算符{{}}

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
 $scope.firstName =false;
    $scope.toggle = function(){
        $scope.firstName =!$scope.firstName ;
    };
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <body>
    <div ng-app="myApp" ng-controller="myCtrl">
      <button ng-click=toggle()>ToGgLe</button>
      <input type="text" ng-disabled="firstName">
   </div>
  </body>
</html>