如何将angular $ scope传递给$ window

时间:2017-10-18 10:50:18

标签: angularjs forms scope

我正在尝试将reCaptcha添加到我的角形。它在recaptcha回调上运行我的表单提交函数“resetPassword”,如下所示,然后在回调上运行$ scope.submitResetForm()。但是当我提交表单时,它会丢失范围,并且无法在页面上找到需要提交的输入字段“forgot_password_dialog_username”。控制台在底部给出错误。有什么建议吗?

HTML

<input name="forgot_password_dialog_username" id="forgot_password_dialog_username" value="" class="form-control" ng-model="forgot_password_dialog_username">

<button
    type="submit"
    class="btn btn-primary btn-lg btn-block top-buffer30 g-recaptcha"
    data-sitekey="<?php echo GOOGLE_RECAPTCHA_INVISIBLE_SITE_KEY; ?>"
    data-callback="resetPassword">
    Reset Password>
</button>

角度代码

$scope.submitResetForm = function($scope) {
    $scope.showErrors = false;
    $scope.shouldShowProfileImage = false;
    if ($scope.forgot_password_dialog_username.length < 3) {
        LoginStateHelper.setStateForNoResetEmailOrUsername($scope);
        return
    }
    $scope.sendingRequest = true;
    $http.get(URL + '/reset/' + $scope.forgot_password_dialog_username).then(function () {
        LoginStateHelper.setStateForPasswordResetResponse($scope);
    });
};

$window.resetPassword = $scope.submitResetForm;

错误

Uncaught TypeError: Cannot read property 'length' of undefined
    at $scope.submitResetForm (LoginForm.js)
    at br.n.Be (recaptcha__en.js)

1 个答案:

答案 0 :(得分:1)

通过此$scope.submitResetForm = function($scope)

更改此$scope.submitResetForm = function()

您不必将$ scope传递给函数。