javascript - 如何以角度 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 文件中。
解决方案
您必须将您的应用程序组件选择器添加到 index.HTML 文件中。通过定位您的应用程序组件来做到这一点。ts 文件夹。@components() 方法中有一个选择器属性。将此复制到 index.html 文件的正文中。例如,如果选择器名称是 app-root。应添加到 index.HTML。
推荐阅读
- javascript - javascript树向特定级别的对象添加新属性
- sql - Oracle SQL 分区范围全部而不是范围单一
- visual-studio - 如何从 Visual Studio 2019 源代码管理身份验证窗口中删除帐户?
- javascript - 获取数值最大的输入元素的输入id
- python - 如何更新通过外键连接的模型中的字段
- c - 将 %d 与 char 一起使用,意外结果
- java - 从 Android Studio 中的不同活动访问布局
- javascript - 用于文本过滤器的 Ag-grid 空白单元格设置
- python - 在 QTableWidget 中搜索
- php - 将 PHP 升级到 8.0 后如何修复 laravel/ui 错误以升级 Laravel