我的应用程序中有一个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>
没有显示错误。有人可以帮我解决这个问题。
答案 0 :(得分:0)
任何有这样问题的人,您都可以像这样在md-not-found属性中构建
<md-not-found>
No matches found.
</md-not-found>