所以使用旧帖子中的代码,我意识到字段电子邮件的值(当我发送到服务器时)是未定义的,所以我尝试了
<!doctype html>
<html data-ng-app="myApp">
<head>
<meta charset="utf-8">
</head>
<body>
<div data-ng-controller="myCtrl">
<form novalidate id="frm-signup" name="addContestantFrm" data-ng-submit="add()">
<div>
<label for="email">Email: *</label>
<input type="email" id="email" name="email" class="input-medium" tabindex="3" title="email" maxlength="255" value="{{contestant.email}}" placeholder="email" data-ng-model="contestant.email" required email-unique />
</div>
<div>
<input type="submit" id="sbmt" name="sbmt" class="input-sbt" data-ng-disabled="!addContestantFrm.$valid" value="Send" />
</div>
</form>
</div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.angularjs.org/1.0.8/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.factory('Contestant',function($http){
return {
checkUniqueEmail : function(email){
return $http.post('./checkemail.php',{email:email});
}
}
});
app.controller('myCtrl',function($scope){
$scope.add = function(){
console.log($scope.contestant);
}
});
app.directive('emailUnique',function(Contestant) {
return {
require: 'ngModel',
link: function(scope, element, attrs,ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
console.log(ctrl.$error.email);
Contestant.checkUniqueEmail(viewValue).success(function (response) {
ctrl.$setValidity('emailUnique', true);
scope.$apply(function(scope) {
ctrl.$setViewValue('email', viewValue);
});
return viewValue;
})
.error(function (data) {
ctrl.$setValidity('emailUnique', false);
console.log(viewValue);
return undefined;
});
});
}
}
});
</script>
</body>
</html>
给我 $digest 已经在进行中
那么如何设置电子邮件值发送到服务器(现在只有一个例子)
更新
我试过如果
(!scope.$$phase) {
//never excuted
}