Javascript angularJS中的客户端重复数据验证
我试图弄清楚是否有可能验证数据客户端,以确保没有重复数据发送到数据库。我有一个angular应用程序,它从api调用获取数据。这是我当前用于添加新主题的控制器(功能完美,但没有数据验证): 以下是我一直在尝试的数据验证:Javascript angularJS中的客户端重复数据验证,javascript,angularjs,validation,Javascript,Angularjs,Validation,我试图弄清楚是否有可能验证数据客户端,以确保没有重复数据发送到数据库。我有一个angular应用程序,它从api调用获取数据。这是我当前用于添加新主题的控制器(功能完美,但没有数据验证): 以下是我一直在尝试的数据验证: angular.module('myApp.controllers') .controller('SubjectNewCtrl', ['$scope', 'SubjectsFactory', '$location', '$route', function ($s
angular.module('myApp.controllers')
.controller('SubjectNewCtrl', ['$scope', 'SubjectsFactory', '$location', '$route',
function ($scope, SubjectsFactory, $location, $route) {
// callback for ng-click 'createNewSubject':
$scope.createNewSubject = function () {
SubjectsFactory.create($scope.subjects);
$location.path('/subjects');
}
}]);
angular.module('myApp.controllers')
.controller('SubjectNewCtrl', ['$scope', 'SubjectsFactory', '$location', '$route',
function ($scope, SubjectsFactory, $location, $route) {
// callback for ng-click 'createNewUser':
$scope.createNewSubject = function () {
var newSubject = $scope.subject.name;
var someSubject = $scope.subjects;
var oldSubject;
if(newSubject){
angular.forEach($scope.subjects, function(allSubjects){
if(newSubject.toLowerCase() == allSubjects.name.toLowerCase()){
oldSubject = true;
}
});
if (!oldSubject){
SubjectsFactory.create($scope.subjects);
}
}
}
}]);
这给了我一个控制台错误-
TypeError:无法读取未定义的
的属性“name”。如何从html中访问新主题的“name”属性?有人能告诉我我所做的是可能的还是有意义的吗?我已经在Angular js中实现了很多次对象创建
我的createNew按钮方法通常只是创建了一个新的javascript对象(),并将scope.currentObject设置为新对象()
在您的情况下,$scope.subject似乎没有初始化为任何内容,因此出现了错误
我猜表单上一定有一个html输入绑定了subject.name字段,但是没有一个subject对象来保存名称,它实际上是未绑定的
如果我希望用户输入一个名称,那么单击“创建”按钮以验证该名称是否未被使用。我会将新名称输入绑定到另一个$scope变量(可能是$scope.newName)
然后在createNewSubject方法中,您可以实际创建一个新主题,如下所示:
$scope.subject = new Object();
$scope.subject.name = $scope.newName;
然后,您可以运行验证代码。如果我正确理解了您的问题,您应该对要验证的特定字段使用指令。一个独特的电子邮件指令就是一个常见的例子。这是我过去用过的一个。没什么特别的
MyApp.directive('uniqueEmail', ['$http', function($http) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
//set the initial value as soon as the input comes into focus
element.on('focus', function() {
if (!scope.initialValue) {
scope.initialValue = ctrl.$viewValue;
}
});
element.on('blur', function() {
if (ctrl.$viewValue != scope.initialValue) {
var dataUrl = attrs.url + "?email=" + ctrl.$viewValue;
//you could also inject and use your 'Factory' to make call
$http.get(dataUrl).success(function(data) {
ctrl.$setValidity('isunique', data.result);
}).error(function(data, status) {
//handle server error
});
}
});
}
};
}]);
然后在标记中可以这样使用它
<input type="text" name="email" ng-model="item.email" data-unique-email="" data-url="/api/check-unique-email" />
<span class="validation-error" ng-show="form.email.$error.isunique && form.email.$dirty">Duplicate Email</span>
重复电子邮件
希望这就是你要找的东西。你能和我一起吃一顿吗?很难理解这一切是怎么联系在一起的。该错误表示“$scope.subject未定义”,$scope.subject是要添加的主题吗?是的,它是要添加的对象。我不知道这是否有道理。我会试着让一个弹夹工作。谢谢,这看起来是我需要做的。如何注入工厂服务?MyApp.directive('uniqueEmail',['myFactory',function(myFactory){…等等。