简体   繁体   中英

Unable to submit form when having Login form

I have the following code and I am unable to trigger - ng-submit or ng-click on this except local onclick function on login button. Please let me knjow if you know good solution or able to identify the problem:

 <form ng-submit = "login.submitLoginForm(login.user)" novalidate> <p> <input class="username-email" type="email" name="username" value="" placeholder="Your Email" ng-model="login.user.userName"> </p> <p> <input class="password" type="password" name="password" value="" placeholder="Password" ng-model="login.user.pwd"> </p> <div class="checkbox"> <label> <input type="checkbox" value="">Remember me</label> </div> <p> <button class="login" type="submit" name="Login" value="Login">Login</button> </p> </form> 

  vm.submitLoginForm = submitLoginForm; function submitLoginForm(user) { // submitLoginForm... console.log("UserName..form.." + user.UserName + 'pwd..' + user.pwd); if(user.userName != '' || user.userName != 'undefined'){ vm.user.userName = user.userName; console.log("UserName..form.." + user.userName + vm.user.userName); ServiceFactory.setUserName(vm.user.userName); } if(user.pwd != '' || user.pwd != 'undefined'){ vm.user.pwd = user.pwd; console.log("UserName..pwd.." + user.pwd + vm.user.pwd); ServiceFactory.setPwd(vm.user.pwd); } initialize(); }; 

 'use strict'; angular .module('app', []) .controller('LoginCtrlr', LoginCtrlr); function LoginCtrlr() { var vm = this; vm.user = { userName: '', pwd: '' }; vm.submitLoginForm = function submitLoginForm(user) { // submitLoginForm... console.log("UserName..form.." + user.userName + ' vm userName: ' + vm.userName); if (user.userName != '' || user.userName != 'undefined') { vm.userName = user.userName; console.log("UserName..form.." + user.userName + ' vm. userName: ' + vm.userName); } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <section class="container" ng-controller="LoginCtrlr as login"> <div> <p class="or">Or</p> <p class="login">your email</p> <form novalidate> <p> <input class="username-email" type="email" name="username" value="" placeholder="Your Email" ng-model="login.user.userName"> </p> <p> <input class="password" type="password" name="password" value="" placeholder="Password" ng-model="login.user.pwd"> </p> <div class="checkbox"> <label> <input type="checkbox" value="">Remember me</label> </div> <p> <button class="login" type="submit" name="Login" value="Login" ng-click="login.submitLoginForm(login.user)">Login</button> </p> <div>{{ login.message }}</div> </form> </div> <p class="note-text-center-xs">By continuing, you accept the terms of Use and Privacy Policy</p> <div class="login-help-center-xs"> <p><a href="#">Forgot your password?</a> </p> </div> <p class="no-account" onclick="#">You Don't have an account yet? Sign up</p> </a> </section> </div> 

I try to create the complete code snipped. the dependency module are removed since not all could access via code snipped.

What I change:

  • define new 'app' module use [] (square bracket)
  • You need to call the controller when you're assigning a object into a model, because you define all controller's method and attribute into it self rather than using $scope. for example:

    <input class="username-email" type="email" name="username" value="" placeholder="Your Email" ng-model="login.user.userName">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM