仅在focusout字段之后调用$scope.watch函数
call $scope.watch function only after focusout of the field
我是angular的新手,我正在寻找一种方法来调用函数,目前在每次更改后调用,只有在焦点离开该字段后才调用。该功能的目的是检查用户是否更改了某个CCD_ 1中的数据,并且只有当他更改了数据时才应执行该功能。现在的问题是,每次按键都会调用该函数,所以如果使用在<td>
中的字符串中添加2个字符,它将执行两次。
这是我的桌子:
<tbody ng-repeat="(user_id,script_id) in data">
<tr ng-repeat="(script_id, cron_format) in script_id">
<td class="userName">{{user(user_id)}}</td>
<td class="scriptName">{{script(script_id)}}</td>
<td class="cronFormat"><input type="text" ng-model="cron_format" ng-change="saveCron(user_id,script_id,cron_format)"/></td>
</tr>
</tbody>
这就是功能:
$scope.$watch('cron_format',function(x,old){
if(x!=old){
}
});
$scope.saveCron = function(userId,scriptId,cronFormat){
$.post("updateCronChange.php",
"user_id="+userId+"&script_id="+scriptId+"&cron_format="+cronFormat,
function(data){
//inside post
alert('cron format changed to:'+cronFormat);
});
}
只有在焦点离开后,我才能调用saveCron()字段(cron_format)??
ngModelOptions
来指示仅在聚焦事件时更新模型:
<input type="text"
ng-model="cron_format"
ng-change="saveCron()"
ng-model-options="{ updateOn: 'focusout', debounce: {'focusout': 0} }"/>
看起来你在谈论ng模糊
https://docs.angularjs.org/api/ng/directive/ngBlur
您可以在视图中使用ng-flur指令,而不是ng-change。这样,每当输入字段失去焦点时,就可以调用saveCron。
您不需要执行$watch
(尽管可以),而是与另一个更改的值进行比较。您可以使用ng-blur
来触发更改检查。
$scope.old = $scope.cron_format = "";
$scope.saveCron(user_id, script_id, cron_format) {
if ($scope.old == $scope.cron_format) {
return; //value was unchanged
}
$scope.old = $scope.cron_format;
$http.post();
});
相关文章:
- 强制 $scope.$watch 只开火一次
- 为什么AngularJS$scope.watch()在我告诉它监视数组时会停止工作
- $location.search() - 如何在 $scope.$watch 函数中使用作用域中的参数
- 仅在focusout字段之后调用$scope.watch函数
- $scope.$watch(var,function) undefined 不是函数
- 不断在 $scope.$watch 函数中收到“类型错误:无法读取 null 的属性'值'”
- 对 angular.extend 的调用不会导致 $scope.$watch proc
- $scope.$watch 使用棱角材料
- AngularJS:$scope.$watch 不会更新从自定义指令$resource获取的值
- 在指令中使用 $scope.$watch 处理未定义变量的替代方法
- 如何在 Angular 1.5 组件中等待绑定(没有 $scope.$watch)
- Scope.watch 在指令中调用在开头 angularjs
- Angular 不会保存 $scope.$watch 顺序
- 如何控制$scope.$watch迭代
- 当我将$scope分配给另一个变量并更改新变量上的监视值时,不会触发角度 $scope.$watch
- 具有许多控件的窗体,它们之间具有交互.比$scope.$watch更清洁的方法
- 为什么当我尝试在 scope.$watch 中运行时,getElementsByTagName 为空
- 如何使用$scope.$watch和ng-repeat
- $scope.watch() 在链接函数中
- AngularJS $scope.$watch Jasmine 测试中未定义的回调函数参数