驗證一個值是否已經存在數據表中
阿新 • • 發佈:2017-05-29
con post link utf-8 程序 部分 ssa lar 準備
前幾天Insus.NET有寫過一篇《angularjs自定義指令Directive》http://www.cnblogs.com/insus/p/6908815.html
僅是在程序中指定某些來值來匹配。
為你的數據表準備一個存儲過程:
判斷是否已經存在此值。只需寫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) {Source Codevar 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; }; } } });
文本框設置自定義指令屬性:
實時演示:
驗證一個值是否已經存在數據表中