2016-09-29 133 views
0

我有我试图收集发送给我的API数据的登录表单,将HTML看起来有点像这样,角度数据不具约束力

<form novalidate ng-controller="loginController" ng-submit="doLogin()"> 
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" ng-model="formData.username"> 
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="formData.password"> 
</form> 

我登录控制器看起来像这样,

var app = app || {}; 
app.controller('loginController', function($scope, $routeParams, authentication) { 
    $scope.formData = {}; 

    $scope.doLogin = function(form) { 
     //authentication.authenticate(); 
     console.log($scope.formData); 
    } 
}); 

doLogin函数在表单提交上运行,但$scope.formData是空的,我希望有一个用户名和密码属性?

+0

你能做出plunker或jfiddle?我认为我们可能需要看到更多的代码。 – defaultcheckbox

+2

您的代码有效,只有用户名需要对范围中的节目有效。 http://jsfiddle.net/Lvc0u55v/10323/ –

回答

0

这里的问题是,你正在使用户名类型=“电子邮件”和HTML5验证正则表达式模式,输入的值必须是有效的,如:“[email protected]”这里我附上一个代码笔的例子:http://codepen.io/alex06/pen/NRvXNm 也关于这个在角文档信息:https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

angular 
    .module('myApp',[]) 
    .controller('loginController', function($scope) { 
    $scope.formData; 

    $scope.doLogin = function(form) { 
     //authentication.authenticate(); 
     console.log($scope.formData); 
    } 
}); 




    <div ng-app="myApp"> 
    <form novalidate ng-controller="loginController" ng-submit="doLogin()" name="myForm"> 
     <input type="email" name="emailInput" class="form-control" id="exampleInputEmail1" placeholder="Enter email" ng-model="formData.username"> 
     <span style="color: red;" ng-show="myForm.emailInput.$error.email">error</span> 
     <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="formData.password"> 
     <input type="submit"> 
    </form> 
    </div>