I have a Directive which goal is to add a suggestion list under a prefix-field-text component. This component is basically a searchbar.
My directive currently look like this (in all my code pieces I removed imports to add readibility):
@Directive({
selector: '[prefixSuggest]',
exportAs: 'prefixSuggest',
host: {
'class': 'prefix-field-suggest__container'
}
})
export class PrefixFieldSuggestDirective implements AfterViewInit {
private componentReference: ComponentRef<PrefixFieldSuggestComponent>;
@Input() fieldTextRef: ElementRef;
@Input() list: Array<PrefixSuggestLineInterface>;
@ViewChild('fieldTextRef', {read: ViewContainerRef}) fieldTextContainer;
constructor(private _injector: Injector, private resolver: ComponentFactoryResolver) {
this.resolver.resolveComponentFactory(PrefixFieldSuggestComponent);
}
ngAfterViewInit(): void {
const prefixFieldSuggestFactory = this.resolver.resolveComponentFactory(PrefixFieldSuggestComponent);
this.componentReference = prefixFieldSuggestFactory.create(this._injector);
this.componentReference.instance.list = this.list;
this.fieldTextContainer.insert(this.componentReference.hostView);
}
}
And my component looks like this :
@Component({
selector: 'prefix-field-suggest',
templateUrl: './prefix-field-suggest.component.html',
styleUrls: ['./prefix-field-suggest.component.scss']
})
export class PrefixFieldSuggestComponent {
/** Item list to display */
@Input() list: Array<PrefixSuggestLineInterface>;
/** Search string typed in search input */
@Input() searchTerm: string;
/** Input ID to align itself beneath */
@Input() inputId: string;
/** Offset between the suggest and the input; default 0 */
@Input() offset: number = 0;
/** Event when an item is selected */
@Output() itemSelected: EventEmitter<any>;
}
The template file for the PrefixFieldSuggestComponent :
<div class="prefix-field-suggest"
[ngStyle]="{ 'top': offset + 'px'}">
<span *ngFor="let item of list">
{{item.title | prefixBoldifyTerm:searchTerm}} {{item.metaData}}
</span>
</div>
the PrefixSuggestLineInterface is just a contract interface so that different people in my team can implement their own suggestion lines, depending on the information they want to display into it. ATM it's basically 2 string fields.
Question : I would like to access to the ViewContainerRef of the prefix-field-ext (searchbar) component, from my directive. I tried many things like #[fieldTextRef], #[fieldTextRef]="mysearchbar", fieldTextRef, etc ....
I tried these possibilities on this simple page :
<div class="prefix-search-group">
<prefix-field-text prefixSuggest #fieldTextRef="prefixSuggest" list="list" [identifier]="search"></prefix-field-text>
</div>
But in every cases, my fieldTextRef Input is always null. (Because it's not a child element). Is it even possible to do what I'm trying to do ?
Thanks a lot for your enlightning.
prefix-field-textcomponent? How areprefix-field-suggestandsearchBarrelated?prefix-field-suggestin your template?