【问题标题】:Calling AngularJS inside <script> tag在 <script> 标签内调用 AngularJS
【发布时间】:2015-05-04 04:44:42
【问题描述】:

我有这个指令:

function Recaptcha(RecaptchaService) {
    return {
        restrict: 'E',
        templateUrl: 'app/components/login/recaptcha.html',
        controller: function($scope) {
            $scope.sendResp = function(response) {
                RecaptchaService.sendResponse(response);
            };
        }
    }
}

使用模板登录/验证码:

<script src='https://www.google.com/recaptcha/api.js'></script>

<script type="text/javascript">
    var verifyCallback = function(response) {
        $scope.sendResp(response);
    };
</script>

<div class="g-recaptcha" data-sitekey="MY-KEY" data-callback="verifyCallback"></div>

所以,我想在我的&lt;script&gt; 中调用$scope.sendResp。但我收到$scope is not defined。有没有办法做到这一点?

【问题讨论】:

  • verifyCallback 添加到控制器并将引用传递给$scopestackoverflow.com/questions/16881478/…
  • 不行,已经试过了
  • 你的意思是 $scope.verifyCallback 在指令中,并在 data-callback 中调用它,对吗? @Pogrindis
  • 据我所知,您无法在模块外访问 $scope。

标签: javascript angularjs angularjs-directive


【解决方案1】:

试试这个..
(仅当验证码 div 在 ng-app 内时才有效)

<script type="text/javascript">
    var verifyCallback = function(response) {
        var el = document.querySelector('.g-recaptcha'),
            $el = angular.element(el),
            $scope = $el.scope();

            $scope && $scope.sendResp(response);
    };
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2021-08-20
    • 1970-01-01
    • 2020-04-19
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多