[英]Custom form error validation in Angular-material ng-messages
視圖中的自動完成表單字段:
<md-autocomplete
md-input-name="cityCode"
...
md-floating-label="Enter city code" required cityCodeInvalid>
<ng-messages="searchForm.cityCode.$error" ng-if="search.cityCode.$touched">
<div ng-message="required">City Code required</div>
<div ng-message="cityCodeInvalid">City code Invalid</div>
</ng-messages>
</md-autocomplete>
JS代碼
var app = angular.module('MyApp',['ngMaterial', 'ngMessages']);
/*
My controller code:
app.controller.....
*/
app.directive('cityCodeInvalid', function() {
return {
restrict: "A",
require: "ngModel",
link: function(scope, element, attributes, ngModel) {
ngModel.$validators.cityCodeInvalid = function(modelValue) {
console.log("In custom validate function");
return true; /* forcibly returning true */
}
}
}
});
從上面的代碼片段中,我試圖為md-autocomplete字段城市代碼創建一個自定義錯誤消息。 我有一個城市代碼數組,所以當用戶故意鍵入一個無效的城市代碼(不是我擁有的代碼列表時)時,我想顯示一個“無效城市代碼”的ng消息
我看到我的代碼無法正常工作,我每次都要強制從自定義驗證函數返回true,只是為了驗證自定義函數(在指令中)是否已被觸發。 我沒有結果。
搜索類似的錯誤。 我認為應該將Angular Directive放在元素“ kebab-case”中。 指令駱駝
app.directive('cityCodeInvalid', function()....
元素烤肉串情況:
<md-floating-label="Enter city code" required city-code-invalid>
我的答案適用於將來的人們,他們正在尋找類似的錯誤並得出結論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.