【问题标题】:Difficulties running JavaScript (from .js file) in an Angular application在 Angular 应用程序中运行 JavaScript(来自 .js 文件)的困难
【发布时间】:2018-02-23 06:09:17
【问题描述】:

我正在尝试合并一个 Javascript 函数(包含在 app.js 中),我试图从我的 Angular 2 应用程序的 index.html 运行该函数。

最初,我使用了一个名为 Office 插件生成器的 CLI 程序来制作一个非 Angular 应用程序,该 JavaScript 在其中工作。

但是,在 Angular 应用程序中使用插件生成器时,不会自动生成 app.js 文件。手动复制粘贴 app.js 文件,<script> 链接也不起作用。我意识到我只提供了几个文件的代码,让我知道我是否应该编辑更多,或者提供一个 github 链接?

chrome 中的错误是 net::ERR_ABORTED 未使用 404 消息定义。 (与app.js文件有关)

~~~~HTML~~~~~

<head>
  <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/1.0/fabric.css">
  <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/1.0/fabric.components.css">
  <meta charset="utf-8">
  <title>Microsoft Graph Connect sample</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>

  <script>
    window.history.replaceState = function(){};
    window.history.pushState = function(){};
  </script>

</head>

<body>
  <app-root></app-root>
  <button onclick="setItemBody()">Paste to email</button>

<script type="text/javascript" src="node_modules/core-js/client/core.js"></script>
<script type="text/javascript" src="node_modules/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="node_modules/office-ui-fabric-js/dist/js/fabric.js"></script>

<script type="text/javascript" src="app.js"></script>

</body>

</html>

~~~~~~~app.js~~~~~~~~

var item;

Office.initialize = function () {
    item = Office.context.mailbox.item;
    // Checks for the DOM to load using the jQuery ready function.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.
        // Set data in the body of the composed item.
       // setItemBody();
    });
}

// Get the body type of the composed item, and set data in 
// in the appropriate data type in the item body.
function setItemBody() {
    item.body.getTypeAsync(
        function (result) {
            if (result.status == Office.AsyncResultStatus.Failed){
                write(result.error.message);
            }
            else {
                // Successfully got the type of item body.
                // Set data of the appropriate type in body.
                if (result.value == Office.MailboxEnums.BodyType.Html) {
                    // Body is of HTML type.
                    // Specify HTML in the coercionType parameter
                    // of setSelectedDataAsync.
                    item.body.setSelectedDataAsync(
                        '<b>These are the times I am available:</b><br>Monday -- 8:30 to 9:00<br>Tuesday -- 1:00 to 5:00<br>Thursday -- 4:00 to 5:00<br>',
                        { coercionType: Office.CoercionType.Html, 
                        asyncContext: { var3: 1, var4: 2 } },
                        function (asyncResult) {
                            if (asyncResult.status == 
                                Office.AsyncResultStatus.Failed){
                                write(asyncResult.error.message);
                            }
                            else {
                                // Successfully set data in item body.
                                // Do whatever appropriate for your scenario,
                                // using the arguments var3 and var4 as applicable.
                            }
                        });
                }
                else {
                    // Body is of text type. 
                    item.body.setSelectedDataAsync(
                        ' Kindly note we now open 7 days a week.',
                        { coercionType: Office.CoercionType.Text, 
                            asyncContext: { var3: 1, var4: 2 } },
                        function (asyncResult) {
                            if (asyncResult.status == 
                                Office.AsyncResultStatus.Failed){
                                write(asyncResult.error.message);
                            }
                            else {
                                // Successfully set data in item body.
                                // Do whatever appropriate for your scenario,
                                // using the arguments var3 and var4 as applicable.
                            }
                         });
                }
            }
        });

}

// Writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

【问题讨论】:

    标签: javascript html angular add-in


    【解决方案1】:

    实际上你不应该把你的 js 文件放在 index.html 中,最好在 .angular-cli.json 文件中添加。关于 js 在 ng2+ 项目中不起作用。查看https://angular.io/guide/attribute-directives 我认为你必须制作包装器。也检查一下https://medium.com/@NetanelBasal/typescript-integrate-jquery-plugin-in-your-project-e28c6887d8dc

    【讨论】:

      猜你喜欢
      • 2023-02-19
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多