在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中

Two function calls on input field, one onBlur and one not in Angular

本文关键字:一个 Blur Angular 函数调用 字段 输入 两个      更新时间:2023-09-26

我有一个typeahead字段,其中包含两个要触发的函数。一个应该是模糊的,这是字段的验证。另一个应该是作为用户类型,这是uib-typeahead使用的函数。

现在我正在使用ng-model-options="{ updateOn: 'blur' }",但它不会在用户键入时触发typeahead。

有没有办法让验证函数正常地触发onBlur和typeahead?

例如,我调用了一个指令编号验证,我想在模糊时调用它。然后在typeahead中,我想在用户键入时调用triggerfunction。

                          <input
                           number-validation
                           type="text"
                           maxlength="10"
                           class="form-control input-sm"
                           ng-model="number"
                           ng-change="number.elsewhere = null"
                           required
                    uib-typeahead="number for number in triggerfunction($viewValue)"
                    typeahead-template-url="/template.html"
                    typeahead-loading="Loadinglocations" typeahead-on-select="SelectFunction($item, 1)" typeahead-wait-ms="3"
                    typeahead-min-length="3">

对于"blur",可以使用"ng blur"。对于预编型功能,请使用"ng keyup"。

这里有一个例子。。。

https://jsfiddle.net/eh2morxb/

这是代码。。。

<input type="text" ng-model="inputText" ng-blur="blurHandler()" ng-keyup="typeAhead()" placeholder="Type something...">
angular.module("myApp", []).controller("myController", ["$scope", function($scope) {
  $scope.inputText = "";
  $scope.typeAheadMessage = "";
  $scope.blurMessage = "";
  $scope.blurHandler = function() {
    $scope.blurMessage = "Perform input validation on " + $scope.inputText;
  };
  $scope.typeAhead = function() {
    $scope.typeAheadMessage = "Perform type-ahead lookup on " + $scope.inputText;
  };
}])

您可以使用ng-blur指令触发模糊事件https://docs.angularjs.org/api/ng/directive/ngBlur