javascript - 如何从 html 文件中获取控制器中 $scope.foo 的值
问题描述
我有一个自定义 html 标签:
<custom-scan id="fingscan" client="12345678" tries="3"></custom-scan>
但是,客户端不应该是一个常量值,而是来自工作流前一部分的搜索结果的变量。
控制器有一个名为的值$scope.userInfo.cu
,它保存我需要设置的值。
因此 :
<custom-scan id="fingscan" client="" tries="3"></custom-scan>
<script>
const fingscan = document.querySelector('custom-scan');
fingscan.setAttribute("client", $scope.userInfo.cu);
console.log(fingscan.getAttribute("client"));
</script>
抛出我一个ReferenceError: $scope is not defined.
,并将变量更改为只是userInfo.cu
抛出我相同,除了说 userInfo 没有定义。
我在这里做错了什么?我的猜测是 js 控制器和 HTML 文件中的脚本彼此不可见。请更新我如何解决这个问题?
解决方案
您需要在 angularsjs 范围内更改它
<script>
var ngApp = angular.module('myNgApp', []);
ngApp.controller('myController', function ($scope) {
const fingscan = document.querySelector('custom-scan');
fingscan.setAttribute("client", $scope.userInfo.cu);
console.log(fingscan.getAttribute("client"));
});
</script>
$scope.apply()
你可能还需要打电话
推荐阅读
- php - PHP APC:缓存被进程锁定,只能同步工作
- css - 单元格中的CSS网格响应图像
- java - 为什么我可以使用 UCanAccess 恢复已删除的记录?
- asp.net-core-webapi - 尽管有 VS 2017 预览版,但 ASP.NET Core Web API 模板中没有个人用户帐户身份验证选项
- python - Kmeans 可视化错误 - 不可散列的类型:'numpy.ndarray'
- abap - BAPI_SALESORDER_CHANGE 不进行更改
- networking - apache2 无法从外部访问他的 IP
- amazon-web-services - API Gateway 代理到 lambda 测试工作但不是端点
- python - 如何在 python 中打印 sqlite3 的输出
- c - 多重定义并确保正确编写函数