Word add-in 对话框未反映从 parent 设置的本地存储值
Word add-in dialog not reflecting the localstorage value set from parent
我在 Word add-in 中使用 office 对话框 api。当我设置本地存储并在对话框打开后读取时,我在对话框中看到了值。但是当对话框打开并且我在 parent 中设置本地存储时,我没有看到它出现在对话框中。我正在使用 localstorage 在启动后与 child 对话框进行通信。
对话框打开后,parent 和 child 之间还有其他通信方式吗?从对话框发送消息到 parent.
时使用 Office.context.ui.messageParent
Main page:
$scope.opensettingDialog = function () {
Office.context.ui.displayDialogAsync("https://localhost/tz/setting", {90,50},
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processSettingDialog);
});}
function processSettingDialog (arg) {
var messageFromDialog = JSON.parse(arg.message);
if (messageFromDialog.messageType == "save") {
//do operations & store result
localStorage.setItem("tbSave", "true");
}
else {
dialog.close();
}
}
Settings Dialog:
$scope.acceptSettings = function () {
var messageObject = {
messageType: "savedoc"
};
var jsonMessage = JSON.stringify(messageObject);
Office.context.ui.messageParent(jsonMessage);
$scope.intervalpromise = $interval(checkSave2Update(), 1000);
};
//Wait for parent operation to complete
var checkSave2Update= function () {
var processingStatus = localStorage.getItem("tbSave");//it never has the value set in the parent page.
if ((processingStatus == "undefined") || (processingStatus == null)) {
$interval.cancel($scope.intervalpromise);
$scope.intervalpromise = $interval(checkSave2Update(), 1000);
return;
}
else {//cancel wait
$interval.cancel($scope.intervalpromise);
}
}
就 localStorage 而言,可能只是您遇到了 Internet Explorer 问题(在桌面上,IE 容器内的 Office 加载项 运行)。
问题和解决方法在 中有详细描述。基本上,本地存储可能会在选项卡之间不同步(这实际上就是任务窗格与对话框的区别)。要解决此问题,您可以在某个键上设置一个值,这将确保刷新 localStorage。
我们在 Script Lab 中使用此解决方法:请参阅 https://github.com/OfficeDev/script-lab/blob/master/packages/common/src/utilities/ensure.fresh.local.storage.ts
希望对您有所帮助。
我在 Word add-in 中使用 office 对话框 api。当我设置本地存储并在对话框打开后读取时,我在对话框中看到了值。但是当对话框打开并且我在 parent 中设置本地存储时,我没有看到它出现在对话框中。我正在使用 localstorage 在启动后与 child 对话框进行通信。 对话框打开后,parent 和 child 之间还有其他通信方式吗?从对话框发送消息到 parent.
时使用 Office.context.ui.messageParentMain page:
$scope.opensettingDialog = function () {
Office.context.ui.displayDialogAsync("https://localhost/tz/setting", {90,50},
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processSettingDialog);
});}
function processSettingDialog (arg) {
var messageFromDialog = JSON.parse(arg.message);
if (messageFromDialog.messageType == "save") {
//do operations & store result
localStorage.setItem("tbSave", "true");
}
else {
dialog.close();
}
}
Settings Dialog:
$scope.acceptSettings = function () {
var messageObject = {
messageType: "savedoc"
};
var jsonMessage = JSON.stringify(messageObject);
Office.context.ui.messageParent(jsonMessage);
$scope.intervalpromise = $interval(checkSave2Update(), 1000);
};
//Wait for parent operation to complete
var checkSave2Update= function () {
var processingStatus = localStorage.getItem("tbSave");//it never has the value set in the parent page.
if ((processingStatus == "undefined") || (processingStatus == null)) {
$interval.cancel($scope.intervalpromise);
$scope.intervalpromise = $interval(checkSave2Update(), 1000);
return;
}
else {//cancel wait
$interval.cancel($scope.intervalpromise);
}
}
就 localStorage 而言,可能只是您遇到了 Internet Explorer 问题(在桌面上,IE 容器内的 Office 加载项 运行)。
问题和解决方法在 中有详细描述。基本上,本地存储可能会在选项卡之间不同步(这实际上就是任务窗格与对话框的区别)。要解决此问题,您可以在某个键上设置一个值,这将确保刷新 localStorage。
我们在 Script Lab 中使用此解决方法:请参阅 https://github.com/OfficeDev/script-lab/blob/master/packages/common/src/utilities/ensure.fresh.local.storage.ts
希望对您有所帮助。