如何检查 angularJs 工厂中的文本框是否为空?

how to check textbox isEmpty in angularJs Factory?

如何检查 angularjs Factory 中的文本框是否为空?

我的 HTML 来源是

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl"> 
    <input type="text" ng-model="test">
</div>
</body>
</html>

在您的控制器中,您可以随时查看模型值。喜欢

var app = angular.module('myApp', []);

app.controller('customersCtrl', function($scope) {

  if($scope.test == ""){
   // textbox is empty, do your stuff here
  }

});
var customersCtrl = function($scope,Validate){

    var isEmpty = Validate.isEmpty($scope.test);

    $scope.Validation = Validate;
    if(isEmpty){
        console.info('Textbox is empty');
    }else{
        console.info('Textbox is not empty');
    }
};


angular.module('myApp').controller('customersCtrl', customersCtrl);

var Validate = function() {

    var factory = {};

    factory.isEmpty = function(val){
        var result = false;
        if(!val){
            result =  true;
        }

        return result;
    };


    return factory;


};



angular.module('myApp').factory('Validate', Validate);

这是根据您的要求Plunker

angular.module('myApp', [])
  .controller('customersCtrl', function($scope){
    $scope.changed = function(){
      if(!$scope.test){
        $scope.msg = 'empty';
      } else {
        $scope.msg = 'not empty';
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="myApp" ng-controller="customersCtrl"> 
   <input type="text" ng-model="test" ng-change="changed()"/>
    {{ test }} | {{ msg }}
 </div>

额外技巧!

您可以在 angular 表达式上查看。请参阅下面的示例!

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope) {
    $scope.test = "";
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl"> 
    <input type="text" ng-model="test">
    <span ng-show="test.length == 0">
  is empty </span>
</div>

</body>
</html>

希望!