首页 > 解决方案 > 如何以角度 4 将 index.html 页面中的值传递给 component.ts 文件

问题描述

您好,我是 Angular 新手,如何将 Index.html 文件连接到 component.ts?请在下面检查我的代码。我在我的 angular index.html 文件中创建了一个名为scannerOutput 的函数。该功能工作正常。但是这个函数很难发送到component.ts。

这是我的 INDEX.HTML

........
<script>
.......
 function scannerOutput(s) {      
 alert(s);
}
 </script>
</head>

我觉得另一种方法直接调用component.ts函数时获取不到值,所以我选择调用索引页再获取。

这里的警报“s”将显示该值,我想将“s”值传递给我的 component.ts 文件。

这里我尝试将调用直接连接到 component.ts 文件:

scannerOutput() {
    scannerOutput();
    function scannerOutput() {    // function definition   
        if (typeof Android !== "undefined" && Android !== null) {
            alert("2nd scan start");
            //console.log("function 1called")
        }
        alert("2nd scan stop");
        console.log("function 3called")
    }
    scannerOutput()

但它不起作用。在这里,我使用 android 设备扫描一个项目并将值发送到这里,但无法将值传递给 component.ts,所以我选择了 index.html。现在我得到了 index.html 中的值,我的问题是我想将该值传输到 component.ts 文件中。

标签: javascripthtmlangulartypescript

解决方案


您必须将您的应用程序组件选择器添加到 index.HTML 文件中。通过定位您的应用程序组件来做到这一点。ts 文件夹。@components() 方法中有一个选择器属性。将此复制到 index.html 文件的正文中。例如,如果选择器名称是 app-root。应添加到 index.HTML。


推荐阅读