如何在Angular JS中的NG模型选项中汇总后运行功能



如果我的输入字段如下一个;

   <input type="text" name="myName" class="form-control" ng-model="Info.personName" ng-model-options="{ debounce: 1000 }" />

我如何在angular.js中运行一个函数以保存用户在输入字段中提供的值

这实际上是AngularJS绑定和ng-model-options的魔法的一部分。

使用ng-model-options debounce,AngularJS在审阅时间经过之前不会"更改"模型的值。其他AngularJS指令(例如ng-change(监视对模型的更改,而不是输入元素的更改。因此,ng-change可用于调用函数,并且该功能不会触发,直到访问出现。

示例:(更改为2000ms以显示明显的延迟(

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.Info = {};
  $scope.changeFunction = function() {
    console.log('change called');
  }
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
</head>
<body ng-controller="MainCtrl">
  <input type="text" name="myName" ng-change="changeFunction()" class="form-control" ng-model="Info.personName" ng-model-options="{ debounce: 2000 }" />
</body>
</html>

http://plnkr.co/edit/ckumqzxojhxrkkhjkmez?p=preview

最新更新