0

我对 ng-disabled 有疑问,它适用于 chrome 和 firefox,但IE 11有问题

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>My first app</title>
    </head>
    <body ng-app="myApp" ng-controller="myController as vm">
        <input type="text" ng-model="vm.name" />
        <button ng-disabled="vm.isBtnDisabled()">Button</button>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <script>
            var app=angular.module("myApp",[]);
            app.controller("myController",["$scope", function($scope){
                this.name="";
                this.isBtnDisabled=function(){
                    return this.name.trim().length==0;
                }
            }]);
        </script>
    </body>
    </html>

如果我犯了任何错误,请告诉我,谢谢。

4

1 回答 1

0

您可以添加原型方法trim以使用 IE11

String.prototype.trim = function() {
     return this.replace(/^\s+|\s+$/g, ''); 
}

var app=angular.module("myApp",[]);
app.controller("myController",["$scope", function($scope){
   this.name="";
   this.isBtnDisabled=function(){
      return this.name.trim().length==0;
   }
}]);
            
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController as vm">
        <input type="text" ng-model="vm.name" />
        <button ng-disabled="vm.isBtnDisabled()">Button</button>
        
    </div>

于 2017-10-04T11:00:26.783 回答