我正在agularJS中迈出第一步,并试图在函数中创建一个静态变量。该函数获取一个名为cron_format
的变量,我希望该函数保存该值。 thee函数检查用户是否更改了cron_format的值,如果是,则发布数据。
这是功能:
$scope.saveCron = function(userId,scriptId,cronFormat,letter,comp,index){
//split cronFormat to match the value in the table, and than save changes
var newCron = $scope.split(cronFormat);
newCron[index] = letter;
//remove commas to match the cron's format
newCron = newCron.toString().replace(/,/g, " ");
if(letter!=comp) {
$.post("updateCronChange.php", "user_id=" + userId + "&script_id=" + scriptId + "&cron_format=" + newCron, function (data) {
console.log("cron changed to: "+newCron);
});
}
}
我想存储变量newCron
,以便下次调用函数时,cronFormat将等于上次执行时的newCron ...任何帮助PLZ?
答案 0 :(得分:1)
使用函数“关闭”外部范围变量的事实,并创建一个来存储以前的值。
var prevCron; // Define a variable to hold the value between function calls.
$scope.saveCron = function(userId,scriptId,cronFormat,letter,comp,index){
//split cronFormat to match the value in the table, and than save changes
var newCron = $scope.split(cronFormat);
newCron[index] = letter;
//remove commas to match the cron's format
newCron = newCron.toString().replace(/,/g, " ");
if(letter!=comp) {
$.post("updateCronChange.php", "user_id=" + userId + "&script_id=" + scriptId + "&cron_format=" + newCron, function (data) {
console.log("cron changed to: "+newCron);
});
}
// Use the outer variable to store our current value.
prevCron = newCron;
}