How to perform the Submit of a form with Onsenui and Angularjs?

Asked

Viewed 91 times

1

How to proceed to perform a Submit with Onsen UI + Angularjs?

I got the following ons-page:

<ons-page ng-controller="LoginController">
    <ons-toolbar>
        <div class="center">Acesso ao Sistema</div>
    </ons-toolbar>

    <form action="@Url.Action("Login", "Account")" method="post" id="loginForm" 
      class="login-form">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "")

        @Html.EditorFor(m => m.Email, new { htmlAttributes = new { 
            @class = "text-input--underbar", 
            placeholder = "E-mail", 
            autofocus = "autofocus" 
        } })
        @Html.ValidationMessageFor(m => m.Email)

        @Html.PasswordFor(m => m.Password, new { 
            @class = "text-input--underbar", 
            placeholder = "Senha" 
        })
        @Html.ValidationMessageFor(m => m.Password)
        <br><br>
        <ons-button modifier="large" class="login-button">Login</ons-button>
        <br><br>
        <ons-button modifier="quiet" class="forgot-password">
            Esqueceu sua senha?
        </ons-button>
    </form>
</ons-page>

@section scripts {
    <script src="~/Static/js/controllers/loginController.js"></script>
}

Plugins render in required order:

<script src="/Static/vendor/angular/angular.min.js"></script>
<script src="/Static/vendor/onsenui/js/onsenui.min.js"></script>    
<script src="/Static/js/controllers/loginController.js"></script>

Content of loginController.js:

(function () {
    var module = ons.bootstrap("loginModule", ["onsen"]);
    //module.controller("AppController", function ($scope) { });
    //module.controller("MainController", function ($scope) { });
    module.controller("LoginController", function ($scope) { });
})();
  • 1

    You want to submit and process the login via AJAX or simply submit the form in the normal way?

  • @Alfred, at first the traditional way. I don’t know how to do it because Onsenui doesn’t generate a Ubmit button. Grateful

1 answer

1


Onsenui doesn’t have a Ubmit button, but you can mask that:

Add a name to your form:

<form action="@Url.Action("Login", "Account")" method="post"
id="loginForm" class="login-form" name="loginForm">

Change your controller to:

module.controller("LoginController", function ($scope) { 
    $scope.login = function() {
        $scope.loginForm.submit();
    }
});

And the login button:

<ons-button modifier="large" class="login-button" ng-click="login()">Login</ons-button>
  • Unfortunately I’m getting the error: TypeError: Cannot read property 'submit' of undefined. Do you know what it could be? Grateful!

  • 1

    I didn’t notice that your form doesn’t have a name. Add to your form name="loginForm": <form action="@Url.Action("Login", "Account")" method="post" id="loginForm" &#xA; class="login-form" name="loginForm">

  • Actually, even with name I kept getting the error TypeError: Cannot read property 'submit' of undefined. So even without the attribute name in tag form, making loginForm.submit(), unused $scope, worked.

  • 1

    That’s funny @Jamestk. The code I posted for you worked and it’s the way I’ve always used it. The error would be justified if your form was outside the Controller, but in this case it is part of the Controller. But, if you managed to make it work, that’s good! I’ll give a study of what happened to you, anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.