0

I have a dropdown (input + autocomplete) where I can type a string or select an object from the dropdown list. I want to use the pipe only when the value in the input is of type string. If user selects a value from dropdown, I need to remove the custom pipe.

I want to apply the custom pipe the typeof option.name is String. and to remove it if type is Object.

Tried the link, but in vain Angular 2 Pipe under condition

Actual code:

<mat-option *ngFor="let option of sortedOptions"
       [innerHTML]="option?.name | highlight : userControl.value" >

What I am not able to understand is how to check String type here:

{{ typeof(option.name) ? (option?.name | highlight : userControl.value)  : option.name }}
4

1 回答 1

1

您可以像这样在组件上创建辅助方法:

isString(val) { return typeof val === 'string'; }

现在您可以像这样检查您的状况:

{{ isString(option.name) ? (option?.name | highlight : userControl.value)  : option.name }}

这是给你的工作演示

于 2019-06-13T11:39:06.083 回答