检查Office.js是否在Office客户端之外加载 [英] Check if Office.js is loaded outside of Office client

查看:206
本文介绍了检查Office.js是否在Office客户端之外加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们在Office客户端之外加载引用office.js的网页,则会收到警告:Office.js is loaded outside of Office client.

If we load a webpage referencing office.js outside Office client, we will get a warning: Office.js is loaded outside of Office client.

此信息很有用.

有人知道我的代码中是否有用于检查该内容的API?

Does anyone know if there is an API to check that inside my code?

我解释了一下我的情况以及为什么问这个问题.我正在使用angularjs制作应用程序,可以将其作为网页加载到浏览器中,也可以作为加载项加载到Office中.而且我意识到我们不应该同时执行<body ng-app="myApp">angular.bootstrap(document, ['myApp']),否则控制器将执行两次.因此,我决定不编写<body ng-app="myApp">,并且在两种情况下(即网页和插件)都始终使用angular.bootstrap.

I explain a little bit my scenario and why I ask this question. I am making an application with angularjs which can be loaded in a browser as a web page or in Office as an add-in. And I realise that we should not do <body ng-app="myApp"> and angular.bootstrap(document, ['myApp']) together, otherwise controllers will execute twice. So I decided to not write <body ng-app="myApp"> and always use angular.bootstrap in both cases (ie, web page & add-in).

因此对于一个网页,我可以写:

So for a web page, I could write:

$(document).ready(function () {
    angular.bootstrap(document, ['myApp'])  
})

app = angular.module('myApp', ['ui.router', 'ui.bootstrap'])
...

因此,对于网页,我需要在Office.initialize内编写angular.bootstrap,并与外接程序共享其他代码:

So for a web page, I need to write angular.bootstrap inside Office.initialize, and share other code with the case of add-in:

Office.initialize = function (reason) {
    $(document).ready(function () {
        angular.bootstrap(document, ['myApp'])
    });
}

app = angular.module('myApp', ['ui.router', 'ui.bootstrap'])
// share the same code

但是,如果我按以下方式将这两种情况写在一起,则它适用于网页,而我给出了

However, if I write these two cases together as follows, it works for a web page, whereas I gives Error: ng:btstrpd App Already Bootstrapped with this Element for add-in.

$(document).ready(function () {
    angular.bootstrap(document, ['myApp'])
    console.log("bootstrapped outside Office.initialize")   
})

Office.initialize = function (reason) {
    $(document).ready(function () {
        angular.bootstrap(document, ['myApp'])
        console.log("bootstrapped inside Office.initialize")
    })
}

app = angular.module('myApp', ['ui.router', 'ui.bootstrap']).

如果设置了标志,控制台将显示bootstrapped outside Office.initialize,然后显示isBootstrapped,然后运行代码将显示Office.contextOffice.context.document未定义:

If I set a flag, console will display bootstrapped outside Office.initialize followed by isBootstrapped, then running the code will show that Office.context or Office.context.document is undefined:

var isBootstrapped = false;

$(document).ready(function () {
    angular.bootstrap(document, ['myApp'])
    isBootstrapped = true
    console.log("bootstrapped outside Office.initialize")
})

Office.initialize = function (reason) {
    $(document).ready(function () {
        if (isBootstrapped) console.log("isBootstrapped")
        else {
            angular.bootstrap(document, ['myApp'])
            console.log("bootstrapped inside Office.initialize")
        }
    })
}

app = angular.module('myApp', ['ui.router', 'ui.bootstrap'])

因此,我确实需要一种有效的方法来检查Office.js是否已加载到Office客户端之外(即,它是网页还是外接程序),以决定应执行angular.bootstrap的哪一部分.

So I really need an efficient way to check if Office.js is loaded outside of Office client (ie, whether it is a web page or an add-in), to decide which piece of angular.bootstrap should be executed.

推荐答案

一种方法是使用 https://github.com/OfficeDev/office-js-helpers .

OfficeHelpers.Utilities.hostOfficeHelpers.Utilities.platform均提供有用的信息.

Both OfficeHelpers.Utilities.host and OfficeHelpers.Utilities.platform provide useful information.

这篇关于检查Office.js是否在Office客户端之外加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆