在<script>标记</script>中调用AngularJS

时间:2015-03-03 13:45:41

标签: javascript angularjs angularjs-directive

我有这个指令:

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

使用模板login / recaptcha:

<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>

所以,我想在$scope.sendResp内拨打<script>。但我得到了$scope is not defined。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:4)

试试这个..
(仅当验证码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>