首页 > 解决方案 > 如何从 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 文件中的脚本彼此不可见。请更新我如何解决这个问题?

标签: javascripthtmlangularjs

解决方案


您需要在 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()你可能还需要打电话


推荐阅读