how to check if username exists in angularjs

时间:2015-06-25 09:58:11

标签: angularjs

All the username and password details are present in oracle DB. I am using servlets and jsp as backend. When user name is registered through a java servlet, it sits in oracle db.

Now I want to check if existing username is already available. I know its like making ajax call to fetch the info but what do I pass as URL? Shall I pass a url pattern of a servlet (example: url:'./getRecords') ? or is there any other way of doing it?

Also using back end as servlet and jsp a good idea? I wonder why people use node js and mondo DB with angular js.

2 个答案:

答案 0 :(得分:1)

我的问题解决了这个问题: 我正在使用 php MVC框架 angularjs

我为验证唯一字段(从DB检查)编写了指令。 这是脚本: 的 app.js

spring.datasource.validationQuery=SELECT 1
spring.datasource.testOnBorrow=true

这是HTML:

var base_url="http://localhost/mysystem/";   
 var angularapp = angular.module('angularapp', ['ngRoute','angularControllers','uniqueField']);  
    angularapp.config(['$routeProvider', function($routeProvider){
        $routeProvider
            .when('/add_user', {
                controller : 'AddUserCtrl',
                templateUrl : base_url+'angular/partials/add_user.php'
            })
            .otherwise({
                redirectTo : '/dashboard'
            });
    }]);

    //unique field   --creating directive
    angular.module('uniqueField', [])
    .directive('uniqueField', function($http) {
      var toId;
      return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, elem, attr, ctrl) { 
          //when the scope changes, check the field.
          scope.$watch(attr.ngModel, function(value) {
            // if there was a previous attempt, stop it.
            if(toId) clearTimeout(toId);

            // start a new attempt with a delay to keep it from
            toId = setTimeout(function(){
              // call to some API that echo "1" or echo "0"
              $http.get(base_url+'controller/check_unique?check=' + value).success(function(data) {

                //set the validity of the field
                if (data == "1") 
                {
                    ctrl.$setValidity('uniqueField', false);
                }
                else if (data == "0")
                {
                    ctrl.$setValidity('uniqueField', true);
                }
              });
            }, 200);
          })
        }
      }
    });

注意:如果用户名已存在,则提交按钮会禁用。 希望这可能有所帮助。

答案 1 :(得分:-1)

后端和前端完全分开。大多数AngularJS调用只是使用$http的标准HTTP请求。服务器正在使用的服务器端语言或数据库并不重要,就前端实现而言,这实际上是一个黑盒子。

您的服务器上需要一个Web界面,可以查询该界面是否已存在用户名。然后,只需使用$http使用所需的用户名作为AngularJS的参数调用该URL。然后,您的服务器端代码将运行查询以确定用户名是否唯一,然后返回有效负载,说明它是否可用。