2016-04-24 66 views

回答

0

您可以使用ng-blur指令。

<input type="text" ng-blur="ValidateBlur()" /> 

https://plnkr.co/edit/hy2sCVjvVqBrm1WSdiBl?p=preview

模塊/控制器設置代碼:

var app = angular.module("testApp", []); 

app.controller("DarrenController", function($scope) { 
    $scope.Text = "Testing..."; 

    $scope.ValidateBlur = function() { 
     alert('Validating'); 
    } 
}); 

HTML:

<body ng-app="testApp"> 
    <div ng-controller="DarrenController"> 
     <input type="text" ng-blur="ValidateBlur()" /> 
    </div> 
    </body> 
+0

看起來不錯,但我怎麼能顯示驗證自定義消息一樣,如果用戶類型的東西,然後如果他刪除,並失去焦點,我需要證明驗證MESSAGE-場空。如果他輸入的值與db值中的值不匹配,那麼我需要顯示 - 「該值在數據庫中不存在」。 – user3395635

+0

@ user3395635 - angular有一個$ http服務,所以如果你的API是REST,你可以使用這個服務來連接並返回數據庫值並將它們傳遞給驗證方法。 https://docs.angularjs.org/api/ng/service/$http –

0
//

HTML

<input type="text" ng-model="search" placeholder="Search ... " ng-focus="focusMsg()" ng-blur="blurMsg()"> 

//控制器

$scope.focusMsg = function() { 
      console.log('Focus'); 
     } 

    $scope.blurMsg = function() { 
      console.log('Blur'); 
     }