-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:autocomplete): support values whit object type (#4996)
close #4981
- Loading branch information
Showing
7 changed files
with
191 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 1 | ||
title: | ||
zh-CN: 使用对象类型选项 | ||
en-US: Use option with object type | ||
--- | ||
|
||
## zh-CN | ||
|
||
当 `nzValue` 和 `ngModel` 类型为 `object` 时使用 `compareWith`([SelectControlValueAccessor](https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection)). | ||
|
||
## en-US | ||
|
||
Use `compareWith`([SelectControlValueAccessor](https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection)) when the `nzValue` and `ngModel` type is `object`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Component, ViewEncapsulation } from '@angular/core'; | ||
|
||
interface Option { | ||
label: string; | ||
value: string; | ||
age: number; | ||
} | ||
|
||
@Component({ | ||
selector: 'nz-demo-auto-complete-object-value', | ||
encapsulation: ViewEncapsulation.None, | ||
template: ` | ||
<div class="example-input"> | ||
<input placeholder="input here" nz-input [(ngModel)]="inputValue" [nzAutocomplete]="auto" /> | ||
<nz-autocomplete #auto [compareWith]="compareFun"> | ||
<nz-auto-option *ngFor="let option of options" [nzValue]="option" [nzLabel]="option.label"> | ||
{{ option.label }} | ||
</nz-auto-option> | ||
</nz-autocomplete> | ||
</div> | ||
` | ||
}) | ||
export class NzDemoAutoCompleteObjectValueComponent { | ||
inputValue: Option = { label: 'Lucy', value: 'lucy', age: 20 }; | ||
options: Option[] = [ | ||
{ label: 'Lucy', value: 'lucy', age: 20 }, | ||
{ label: 'Jack', value: 'jack', age: 22 } | ||
]; | ||
|
||
compareFun = (o1: Option | string, o2: Option) => { | ||
if (o1) { | ||
return typeof o1 === 'string' ? o1 === o2.label : o1.value === o2.value; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters