简体   繁体   English

HTML导入angular2应用程序并传递参数

[英]HTML import angular2 app and pass parameter

I want to use webcomponents and HTML import to import an angular2 module into another webapplication which do not use angular2.我想使用 webcomponents 和 HTML 导入将 angular2 模块导入另一个不使用 angular2 的 web 应用程序。 I know HTML import is only natively supported in few browsers but i will use the polymer framework to pollify other browsers.我知道只有少数浏览器本身支持 HTML 导入,但我将使用聚合物框架来轮询其他浏览器。

I can import the angular app but i'm unable to pass parameters to the angular app from my web app that imports the angular app.我可以导入 angular 应用程序,但无法从导入 angular 应用程序的 Web 应用程序将参数传递给 angular 应用程序。 I'm trying this:我正在尝试这个:

 <dom-module id="sale-stats"> <link rel="import" href="../bower_components/polymer/polymer.html"> <template> <!-- contains my angular app bundled js files --> <link rel="import" href="imports.html"> {{saleid}} <!-- this displays the saleid just to see that the parameter is passed to the webcomponent --> <app-root saleid='{{saleid}}'>Loading... </app-root> </template> <script> HTMLImports.whenReady(function () { Polymer({ is: 'sale-stats', properties: { saleid: { type: String, value: '0' } }, }); }); </script> </dom-module> <script src="/Scripts/webcomponentsjs/webcomponents-lite.js"></script> <link rel="import" href="http://localhost:9600/salestats/index.html" /> <sale-stats saleid="1234567"></sale-stats>

How do i pass parameters to my angular app when using the angular app as a webcomponent that is imported into another app?将 angular 应用程序用作导入到另一个应用程序的 Web 组件时,如何将参数传递给我的 angular 应用程序? Or is it just completely wrong to try and import an angular app as an webcomponent?还是尝试将 angular 应用程序作为 Web 组件导入是完全错误的?

In order to polyfill HTML Imports, you just have to include HTMLImports.js which is available in the webcomponents.js repository.为了HTMLImports.js HTML Imports,你只需要包含在 webcomponents.js 存储库中可用的 HTMLImports.js。 You can install it with bower (or npm ):您可以使用bower (或npm )安装它:

bower install webcomponentsjs

Just include the polyfill before the <link> element:只需在<link>元素之前包含 polyfill:

<script src=".../bower_components/webcomponentsjs/HTMLImports.js"></script>

To wait for the imported file to be loaded, you can use the Promise, or instead wait for the standard onload event.要等待加载导入的文件,您可以使用 Promise,或者等待标准的onload事件。 For example:例如:

<script src=".../bower_components/webcomponentsjs/HTMLImports.js"></script>
<link rel="import" href="imports.html" onload="run( 1234567 )">

Inside the imported HTML file:在导入的 HTML 文件中:

<!-- contains my angular app bundled js files -->
<script>
    function run ( id ) 
    {
        var app = document.createElement( 'app-root' )
        app.setAttribute( 'saleid', id )
        document.body.appendChild( app )                
    }
</script>

Notes: you can also place the onload callback function in the main document.注意:您也可以在主文档中放置onload回调函数。 Also, the import is synchronous on native implementation (Chrome and Opera);此外,导入在本机实现(Chrome 和 Opera)上是同步的; use async in <link> if you don't want to block the parsing.如果您不想阻止解析,请在<link>使用async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM