我有一个简单的自定义指令,其中包含对 url 的 http post 调用。单击提交按钮时,它应该调用 url,因为自定义指令属性放置在标记内。
我在 chrome 控制台中检查了它没有被调用。我不确定我哪里出错了。
代码:
<!DOCTYPE html>
<html ng-app="myApp" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script>
var app = angular.module('myApp',[]);
app.directive('sendMail', function ($http) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
$http.post('MailSenderServlet.do').success(function (data) {
});
}
};
});
</script>
<title>Registration Form</title>
</head>
<body ng-controller="MainCtrl">
<div class="container">
<h2 class="text-muted">Registration form</h2><br/>
<div>
<form name="myForm" action="RegistrationServlet.do" method="POST" novalidate>
<div class="form-group has-feedback">
<label class="control-label">First name:</label> <input type="text" class="form-control input-sm " name="uname" ng-pattern="/^[a-zA-Z]{3,20}/" ng-model="user.uname" placeholder="First Name" required/>
<span style="color:red" ng-show="myForm.uname.$error.pattern">First name cannot be less than 3 letters with no digits</span>
<span style="color:red" class="error" ng-if="myForm.$submitted && myForm.uname.$error.required">Please fill field above<br></span>
<span style="color:red" class="hide-while-in-focus" ng-show="myForm.uname.$error.unique">Username already exist<br/></span>
<span ng-if="myForm.uname.$valid" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
</div>
<button class="form-control btn btn-success" type="submit" name="submit" ng-model="submit" send-mail ">Submit</button>
</form>
</div>
</body>
</html>