Input输入框

    • 需要用户输入表单域内容时。
    • 提供组合型输入框,带搜索的输入框,还可以进行大小选择。

    想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

    基本使用。

    1. @Component({
    2. selector: 'nz-demo-input-basic',
    3. template: `
    4. <input nz-input placeholder="Basic usage" [(ngModel)]="value" />
    5. <br />
    6. <br />
    7. <input nz-input placeholder="Basic usage" [(ngModel)]="value" [disabled]="true" />
    8. `
    9. })
    10. export class NzDemoInputBasicComponent {
    11. value: string;
    12. }

    Input输入框 - 图2

    前置/后置标签

    用于配置一些固定组合。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-addon',
    4. template: `
    5. <div style="margin-bottom: 16px;">
    6. <nz-input-group nzAddOnBefore="Http://" nzAddOnAfter=".com">
    7. <input type="text" nz-input [(ngModel)]="inputValue" />
    8. </nz-input-group>
    9. </div>
    10. <div style="margin-bottom: 16px;">
    11. <nz-input-group [nzAddOnBefore]="addOnBeforeTemplate" [nzAddOnAfter]="addOnAfterTemplate">
    12. <input type="text" nz-input [(ngModel)]="inputValue" />
    13. </nz-input-group>
    14. <ng-template #addOnBeforeTemplate>
    15. <nz-select [ngModel]="'Http://'">
    16. <nz-option [nzLabel]="'Http://'" [nzValue]="'Http://'"></nz-option>
    17. <nz-option [nzLabel]="'Https://'" [nzValue]="'Https://'"></nz-option>
    18. </nz-select>
    19. </ng-template>
    20. <ng-template #addOnAfterTemplate>
    21. <nz-select [ngModel]="'.com'">
    22. <nz-option [nzLabel]="'.com'" [nzValue]="'.com'"></nz-option>
    23. <nz-option [nzLabel]="'.jp'" [nzValue]="'.jp'"></nz-option>
    24. <nz-option [nzLabel]="'.cn'" [nzValue]="'.cn'"></nz-option>
    25. <nz-option [nzLabel]="'.org'" [nzValue]="'.org'"></nz-option>
    26. </nz-select>
    27. </ng-template>
    28. </div>
    29. <div style="margin-bottom: 16px;">
    30. <nz-input-group [nzAddOnAfterIcon]="'setting'">
    31. <input type="text" nz-input [(ngModel)]="inputValue" />
    32. </nz-input-group>
    33. </div>
    34. `
    35. })
    36. export class NzDemoInputAddonComponent {
    37. inputValue: string = 'my site';
    38. }

    带有搜索按钮的输入框。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-search-input',
    4. template: `
    5. <nz-input-group [nzSuffix]="suffixIconSearch">
    6. <input type="text" nz-input placeholder="input search text" />
    7. </nz-input-group>
    8. <ng-template #suffixIconSearch>
    9. <i nz-icon nzType="search"></i>
    10. </ng-template>
    11. <br />
    12. <br />
    13. <nz-input-group nzSearch [nzAddOnAfter]="suffixIconButton">
    14. <input type="text" nz-input placeholder="input search text" />
    15. </nz-input-group>
    16. <ng-template #suffixIconButton>
    17. <button nz-button nzType="primary" nzSearch><i nz-icon nzType="search"></i></button>
    18. </ng-template>
    19. <br />
    20. <br />
    21. <nz-input-group nzSearch nzSize="large" [nzAddOnAfter]="suffixButton">
    22. <input type="text" nz-input placeholder="input search text" />
    23. </nz-input-group>
    24. <ng-template #suffixButton>
    25. <button nz-button nzType="primary" nzSize="large" nzSearch>Search</button>
    26. </ng-template>
    27. `
    28. export class NzDemoInputSearchInputComponent {}

    Input输入框 - 图4

    适应文本高度的文本域

    在输入框上添加前缀或后缀图标。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-presuffix',
    4. template: `
    5. <nz-input-group [nzSuffix]="suffixTemplate" [nzPrefix]="prefixTemplate">
    6. <input type="text" nz-input placeholder="Enter your username" />
    7. </nz-input-group>
    8. <ng-template #prefixTemplate><i nz-icon nzType="user"></i></ng-template>
    9. <ng-template #suffixTemplate
    10. ><i nz-icon nz-tooltip nzTitle="Extra information" nzType="info-circle"></i
    11. ></ng-template>
    12. `
    13. })
    14. export class NzDemoInputPresuffixComponent {}

    Input输入框 - 图6

    带移除图标

    带移除图标的输入框,点击图标删除所有内容。

    1. @Component({
    2. selector: 'nz-demo-input-allow-clear',
    3. template: `
    4. <nz-input-group [nzSuffix]="suffixTemplate">
    5. <input type="text" nz-input [(ngModel)]="inputValue" placeholder="input with clear icon" />
    6. </nz-input-group>
    7. <ng-template #suffixTemplate
    8. ><i
    9. nz-icon
    10. nz-tooltip
    11. class="ant-input-clear-icon"
    12. nzTheme="fill"
    13. nzType="close-circle"
    14. *ngIf="inputValue"
    15. (click)="inputValue = null"
    16. ></i
    17. ></ng-template>
    18. `
    19. })
    20. export class NzDemoInputAllowClearComponent {
    21. inputValue: string | null;
    22. }

    我们为 nz-input 输入框定义了三种尺寸(大、默认、小),高度分别为 40px32px24px

    注意: 在表单里面,我们只使用大尺寸的输入框。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-size',
    4. template: `
    5. <div class="example-input">
    6. <input nz-input placeholder="large size" nzSize="large" />
    7. <input nz-input placeholder="default size" nzSize="default" />
    8. <input nz-input placeholder="small size" nzSize="small" />
    9. </div>
    10. `,
    11. styles: [
    12. `
    13. .example-input .ant-input {
    14. width: 200px;
    15. margin: 0 8px 8px 0;
    16. }
    17. `
    18. ]
    19. })
    20. export class NzDemoInputSizeComponent {}

    Input输入框 - 图8

    输入框的组合展现。

    注意:使用 nzCompact 模式时,不需要通过 nz-col 来控制宽度。

    文本域

    用于多行输入。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-textarea',
    4. template: `
    5. <textarea rows="4" nz-input [(ngModel)]="inputValue"></textarea>
    6. `
    7. })
    8. export class NzDemoInputTextareaComponent {
    9. inputValue: string;
    10. }

    Input输入框 - 图10

    结合 Tooltip 组件,实现一个数值输入框,方便内容超长时的全量展现。

    1. import { Component, ElementRef, ViewChild, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-tooltip',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <input
    7. #inputElement
    8. style="width: 120px"
    9. nz-input
    10. nz-tooltip
    11. nzTrigger="focus"
    12. nzPlacement="topLeft"
    13. nzOverlayClassName="numeric-input"
    14. [nzTitle]="title"
    15. placeholder="Input a number"
    16. [ngModel]="value"
    17. (blur)="onBlur()"
    18. />
    19. `,
    20. styles: [
    21. `
    22. .numeric-input .ant-tooltip-inner {
    23. min-width: 32px;
    24. min-height: 37px;
    25. }
    26. .numeric-input .numeric-input-title {
    27. font-size: 14px;
    28. }
    29. `
    30. ]
    31. })
    32. export class NzDemoInputTooltipComponent {
    33. value = '';
    34. title = 'Input a number';
    35. @ViewChild('inputElement', { static: false }) inputElement: ElementRef;
    36. onChange(value: string): void {
    37. this.updateValue(value);
    38. }
    39. // '.' at the end or only '-' in the input box.
    40. onBlur(): void {
    41. if (this.value.charAt(this.value.length - 1) === '.' || this.value === '-') {
    42. this.updateValue(this.value.slice(0, -1));
    43. }
    44. }
    45. updateValue(value: string): void {
    46. const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
    47. if ((!isNaN(+value) && reg.test(value)) || value === '' || value === '-') {
    48. this.value = value;
    49. }
    50. this.inputElement.nativeElement.value = this.value;
    51. this.updateTitle();
    52. }
    53. updateTitle(): void {
    54. this.title = (this.value !== '-' ? this.formatNumber(this.value) : '-') || 'Input a number';
    55. }
    56. formatNumber(value: string): string {
    57. const string = `${value}`;
    58. const list = string.split('.');
    59. const prefix = list[0].charAt(0) === '-' ? '-' : '';
    60. let num = prefix ? list[0].slice(1) : list[0];
    61. let result = '';
    62. while (num.length > 3) {
    63. result = `,${num.slice(-3)}${result}`;
    64. num = num.slice(0, num.length - 3);
    65. }
    66. if (num) {
    67. result = num + result;
    68. }
    69. return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
    70. }
    71. }

    密码框。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-password-input',
    4. template: `
    5. <nz-input-group [nzSuffix]="suffixTemplate">
    6. <input
    7. [type]="passwordVisible ? 'text' : 'password'"
    8. nz-input
    9. placeholder="input password"
    10. [(ngModel)]="password"
    11. />
    12. </nz-input-group>
    13. <ng-template #suffixTemplate>
    14. <i nz-icon [nzType]="passwordVisible ? 'eye-invisible' : 'eye'" (click)="passwordVisible = !passwordVisible"></i>
    15. </ng-template>
    16. `,
    17. styles: [
    18. `
    19. i {
    20. cursor: pointer;
    21. }
    22. `
    23. ]
    24. })
    25. export class NzDemoInputPasswordInputComponent {
    26. passwordVisible = false;
    27. }

    nz-input-groupcomponent

    参数说明类型默认值
    [nzAddOnAfter]带标签的 input,设置后置标签,可以与 nzAddOnBefore 配合使用string | TemplateRef<void>-
    [nzAddOnBefore]带标签的 input,设置前置标签,可以与 nzAddOnBefore 配合使用string | TemplateRef<void>-
    [nzPrefix]带有前缀图标的 input,可以与 nzSuffix 配合使用string | TemplateRef<void>-
    [nzSuffix]带有后缀图标的 input,可以与 nzPrefix 配合使用string | TemplateRef<void>-
    [nzCompact]是否用紧凑模式booleanfalse
    [nzSearch]是否用搜索框booleanfalse
    [nzSize]nz-input-group 中所有的 nz-input 的大小'default'