【问题标题】:Custom error message not showing自定义错误消息未显示
【发布时间】:2018-06-29 21:09:02
【问题描述】:

我的应用中有一个 AngularJS 自动完成输入组件。我想根据控制器中的条件显示自定义错误消息。这是我尝试过的。

这是我的控制器。

ctrl.searchAddresses = function(searchAddresses) {
return addressService.searchAddresses(searchAddresses)
  .then((response) => { 

    if(response.length == 0) {
      $scope.addressNotMatch = true;

    }

    residentForm.serviceAddress.$setValidity('validationError', true);
    return response;

  }, (error) => { 
    $scope.resident.serviceAddress = '';
})}

在这里,我正在做的是通过返回地址列表的服务调用 API。如果地址的长度为零,我想在我的视图中显示一条错误消息“未找到地址”。

这是我认为的自动完成组件

        <md-autocomplete class="cl-autocomplete" lex md-require-match md-selected-item="resident.serviceAddress" md-input-name="serviceAddress" md-search-text="addressSearchText" placeholder="Service Address *" md-items="address in $residentHomeController.searchAddresses(addressSearchText)"
            md-item-text="address" md-selected-item-change="$residentHomeController.selectedAddress(resident.serviceAddress)" ng-required="true" md-input-minlength="1" md-delay="1000">
            <md-item-template>
                {{address}}
            </md-item-template>
            <div ng-messages="residentForm.serviceAddress.$error" ng-if="residentForm.serviceAddress.$touched">
                <div ng-message="required">Address is required</div>
            </div>
            <div ng-messages="residentForm.serviceAddress.$error" ng-if="addressNotMatch">
                <div ng-message="validationError">No match found</div>
            </div>
        </md-autocomplete>

没有显示错误。有人可以帮我解决这个问题。

【问题讨论】:

  • @sajeetharan 请帮忙
  • 我猜 ng-messages 指令负责可见性标准。您可以尝试删除 ng-if 吗?
  • @Rakesh Burbure 也试过了。还是不行
  • 我猜 ng-message 参数有问题。在您的代码中,请检查您在 residentForm.serviceAddress.$error 中获得的值。我相信没有名称validationError的字段。
  • 现在工作了吗?

标签: angularjs angularjs-material


【解决方案1】:

任何遇到此类问题的人,您都可以像这样简单地构建 md-not-found 属性

   <md-not-found>
    No matches found.
  </md-not-found>

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2016-09-19
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多