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 フィールドの都市コードのカスタム エラー メッセージを作成しようとしています。私は都市コードの配列を持っているので、ユーザーが私が持っているコードのリストにない無効な都市コードを故意に入力すると、「無効な都市コード」というメッセージを表示したかった

コードが機能していないことがわかりました。カスタム関数 (ディレクティブ内) がトリガーされたかどうかを確認するためだけに、毎回カスタム検証関数から true を返すように強制しました。結果が表示されません。

4

1 に答える 1