2015-03-03 62 views
0

調用AngularJS我有這樣的指令:內<script>標籤

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> 

所以,我想打電話給我$scope.sendResp裏面<script>。但我得到$scope is not defined。有沒有辦法做到這一點?

+2

添加'verifyCallback'到控制器,並通過在裁判'$ scope'? http://stackoverflow.com/questions/16881478/how-to-call-a-method-defined-in-an-angularjs-directive – Pogrindis 2015-03-03 13:49:36

+0

doens't工作,已經試過 – 2015-03-03 14:07:45

+0

你的意思是$ scope.verifyCallback指令裏面,並在數據回調中調用它,對吧? @Pogrindis – 2015-03-03 14:08:21

回答

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>