视图中的自动完成表单字段:
<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,只是为了验证自定义函数(在指令中)是否已被触发。我看不到任何结果。