我是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();
});