检查angularjs脚本中的输入字段是否为空

时间:2016-07-11 04:40:14

标签: angularjs-scope

我有一个HTML代码,如何检查脚本中的输入字段是否为空。我们有什么方法可以使用范围来检查这个。我知道我们有角度的形式验证,但我只是好奇。

1 个答案:

答案 0 :(得分:1)

要达到预期效果,您可以使用ng-keyup功能

HTML:

<html lang="en">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <form>
    First Name: <input type="text" ng-keyup="check()" ng-model="firstname">
  </form>
</div>

</body>
</html>

JS:

var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.check= function(){
      var x =$scope.firstname;
      if(x.length==''){
        alert("input empty");
      }
    };
});

Codepen- http://codepen.io/nagasai/pen/qNPJZV