0

视图中的自动完成表单字段:

<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,只是为了验证自定义函数(在指令中)是否已被触发。我看不到任何结果。

4

1 回答 1

0

搜索类似的错误。我认为Angular Directive应该放在元素“kebab-case”中。指令骆驼案例:

app.directive('cityCodeInvalid', function()....

元素烤肉串案例:

<md-floating-label="Enter city code" required city-code-invalid>

我的答案是为将来寻找类似错误并结束此问题的人准备的。

于 2016-08-11T17:30:16.767 回答