francdore
francdore

Reputation: 315

How to pass angular $scope to $window

I'm trying to add reCaptcha to my angular form. It runs my form submit function "resetPassword" on the recaptcha callback like below which then on callback runs $scope.submitResetForm(). But when I submit the form, it loses the scope and the input field "forgot_password_dialog_username" that needs to be submitted can't be found on the page. The console gives the error right at the bottom. Any suggestions?

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>

Angular code

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

Error

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

Upvotes: 0

Views: 161

Answers (1)

BEN AHMED EL HABIB
BEN AHMED EL HABIB

Reputation: 29

change this $scope.submitResetForm = function($scope) by this $scope.submitResetForm = function()

you don't have to passe the $scope into the function.

Upvotes: 1

Related Questions