验证一个值是否已经存在数据表中

栏目: 编程语言 · AngularJS · 发布时间: 8年前

内容简介:验证一个值是否已经存在数据表中

仅是在程序中指定某些来值来匹配。

为你的数据表准备一个存储过程:

验证一个值是否已经存在数据表中

判断是否已经存在此值。只需写SELECT语句。如果没有记录返回,它将返回null。

有ASP.NET MVC程序中,创建model:

验证一个值是否已经存在数据表中

还要创建一个实体方法,是程序与数据协作与沟通的:

验证一个值是否已经存在数据表中

程序逻辑部分,还要写一个接口,供前端angularjs去读取:

验证一个值是否已经存在数据表中

返回给前端exists布尔值。

Ok,进行到前端angularjs时,写自定义指令Directive:

验证一个值是否已经存在数据表中

airExpressApp.directive('validateCountry', function ($http, $q) {
    return {
        restrict: 'AE',
        require: 'ngModel',
        link: function ($scope, element, attributes, ngModelController) {
            ngModelController.$asyncValidators.countryExists = function (modelValue, viewValue) {
                var deferred = $q.defer();

                var obj = {};
                obj.Country_EN = modelValue;
                $http({
                    method: 'POST',
                    url: '/Code/CountryByKey',
                    dataType: 'json',
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    data: JSON.stringify(obj),
                }).then(function (response) {
                    if (response.data.exists) {
                        deferred.resolve();
                    } else {
                        deferred.reject();
                    }
                });
                return deferred.promise;
            };
        }
    }
});

Source Code

文本框设置自定义指令属性:

验证一个值是否已经存在数据表中

实时演示:

验证一个值是否已经存在数据表中

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security Testing Cookbook

Web Security Testing Cookbook

Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99

Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具