What is the way to use the MAT_AUTOCOMPLETE_DEFAULT_OPTIONS injection token, or the const AUTOCOMPLETE_OPTION_HEIGHT to customize mat-autocomplete. These constants, among others are exposed in the public API here but no documentation on how to use them
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
It's quite weird, why such a needed option like the height of item (option) isn't defined as input.
Because without changing of
AUTOCOMPLETE_OPTION_HEIGHT
we will get a bug (https://github.com/angular/components/issues/18030)Here is a possible solution:
Step 1. Change style.It's the easiest moment. We can just change the global style. Let's use 24px instead 48px
Step 2. Overwrite original_scrollToOption
method ofMatAutocompleteTrigger
The pain is located here: https://github.com/angular/components/blob/49a1324acc05cec1c5ff28d729abfe590f6772dd/src/material/autocomplete/autocomplete-trigger.ts line 498, method
_scrollToOption
.This method takes two hardcoded constants (
AUTOCOMPLETE_OPTION_HEIGHT
,AUTOCOMPLETE_PANEL_HEIGHT
) and uses it to calculate new scroll position. Sure, if you have not 48px, but less or more - it will work incorrectly.So, we have to overwrite this method.
The most elegant way to do it - using
Step 2.1. Create directiveDirective
(keep in mind, we can get access with@ViewChild()
toMatAutocompleteTrigger
and change it in the scope of a parent component, but it's not a reusable solution)._scrollToOption
method. Only one thing we do - avoid using constants:AUTOCOMPLETE_OPTION_HEIGHT
,AUTOCOMPLETE_PANEL_HEIGHT
optionHeight
andpanelHeight
AUTOCOMPLETE_OPTION_HEIGHT
,AUTOCOMPLETE_PANEL_HEIGHT
Now we can modify our template and add a new directive and addition inputs.
Note. Our directive
matAutocompleteTriggerAccessor
and inputoptionHeight
should be attached to<input>
, but not to<mat-autocomplete>
.And that's all. Now all works as it should.
Here is a full solution: https://stackblitz.com/edit/angular-cn4ox8
Add
MAT_AUTOCOMPLETE_DEFAULT_OPTIONS
to your module'sproviders
array like this:As for the other constants, I'm not sure you can adjust them. You'll probably have to play around with the CSS styles, see for instance this issue for adjusting the panel height.