Angularjs: Show search result in typeahead if no match is found for the entered string

I am using Angular UI-bootstrap typeahead.

I have HTML

<input type="text" 
       ng-init="selectedCrossFormFieldText = selectedCrossFormFieldText || {}" 
       ng-model="selectedCrossFormFieldText[fieldId]" 
       placeholder="Auto-complete data from field of form" 
       empty-typeahead 
       ng-trim="false" 
       typeahead="crossFormField.id as crossFormField for crossFormField in getCrossFormFieldData(fieldId, $viewValue) | filter:$viewValue:optionComparator" 
       typeahead-template-url="views/blocks/cross_form_data_typeahead.html" 
       typeahead-on-select="addCrossFormField(fieldId, $item, $model, $label)" 
       typeahead-loading="loadingCrossFormField[fieldId]"  
       typeahead-min-length="0" class="form-control"  />

      

and

Script:

.then(function (response) {
    console.log('DataForTypeahead', response);
    return response.data;
});

      

When I type, it displays the appropriate options in the typeahead popup. But what should I do to show when no matches are found on the same No Matches popup?

+3


source to share


1 answer


You can check the size of the response.data file. If it's 0, push the item into the array:

.then(function (response) {
    console.log('DataForTypeahead', response);
    if (response.data.length === 0) {
        response.data.push({
            label: 'No matches found'
        })
    }
    return response.data;
});

      



You might want to change your code like:

typeahead="crossFormField.id as crossFormField.label for crossFormField in getCrossFormFieldData(fieldId, $viewValue) | filter:$viewValue:optionComparator"

      

+4


source







All Articles