Button按钮

按钮用于开始一个即时操作。

何时使用

标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。

代码演示

Button按钮 - 图1

按钮类型

按钮有四种类型:主按钮、次按钮、虚线按钮、危险按钮。主按钮在同一个操作区域最多出现一次。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-basic',
  4. template: `
  5. <button nz-button nzType="primary">Primary</button>
  6. <button nz-button nzType="default">Default</button>
  7. <button nz-button nzType="dashed">Dashed</button>
  8. <button nz-button nzType="danger">Danger</button>
  9. `,
  10. styles: [
  11. `
  12. [nz-button] {
  13. margin-right: 8px;
  14. margin-bottom: 12px;
  15. }
  16. `
  17. ]
  18. })
  19. export class NzDemoButtonBasicComponent {}

Button按钮 - 图2

按钮尺寸

按钮有大、中、小三种尺寸。

通过设置 nzSizelargesmall 分别把按钮设为大、小尺寸。若不设置 nzSize,则尺寸为中。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-size',
  4. template: `
  5. <nz-radio-group [(ngModel)]="size">
  6. <label nz-radio-button nzValue="large">Large</label>
  7. <label nz-radio-button nzValue="default">Default</label>
  8. <label nz-radio-button nzValue="small">Small</label>
  9. </nz-radio-group>
  10. <br />
  11. <br />
  12. <button nz-button [nzSize]="size" nzType="primary">Primary</button>
  13. <button nz-button [nzSize]="size" nzType="default">Default</button>
  14. <button nz-button [nzSize]="size" nzType="dashed">Dashed</button>
  15. <button nz-button [nzSize]="size" nzType="danger">Danger</button>
  16. <br />
  17. <button nz-button nzType="primary" [nzSize]="size" nzShape="circle"><i nz-icon type="download"></i></button>
  18. <button nz-button nzType="primary" [nzSize]="size" nzShape="round"><i nz-icon type="download"></i>Download</button>
  19. <button nz-button nzType="primary" [nzSize]="size"><i nz-icon type="download"></i>Download</button>
  20. <br />
  21. <nz-button-group [nzSize]="size">
  22. <button nz-button nzType="primary"><i nz-icon type="left"></i>Backward</button>
  23. <button nz-button nzType="primary">Forward<i nz-icon type="right"></i></button>
  24. </nz-button-group>
  25. `,
  26. styles: [
  27. `
  28. [nz-button] {
  29. margin-right: 8px;
  30. margin-bottom: 12px;
  31. }
  32. nz-button-group [nz-button] {
  33. margin-right: 0;
  34. }
  35. `
  36. ]
  37. })
  38. export class NzDemoButtonSizeComponent {
  39. size = 'large';
  40. }

Button按钮 - 图3

加载中状态

添加 nzLoading 属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-loading',
  4. template: `
  5. <button nz-button nzType="primary" nzLoading><i nz-icon type="poweroff"></i>Loading</button>
  6. <button nz-button nzType="primary" nzSize="small" nzLoading>Loading</button>
  7. <br />
  8. <button nz-button nzType="primary" (click)="loadOne()" [nzLoading]="isLoadingOne">Click me!</button>
  9. <button nz-button nzType="primary" (click)="loadTwo()" [nzLoading]="isLoadingTwo">
  10. <i nz-icon type="poweroff"></i>Click me!
  11. </button>
  12. <br />
  13. <button nz-button nzLoading nzShape="circle"></button>
  14. <button nz-button nzLoading nzType="primary" nzShape="circle"></button>
  15. `,
  16. styles: [
  17. `
  18. [nz-button] {
  19. margin-right: 8px;
  20. margin-bottom: 12px;
  21. }
  22. `
  23. ]
  24. })
  25. export class NzDemoButtonLoadingComponent {
  26. isLoadingOne = false;
  27. isLoadingTwo = false;
  28. loadOne(): void {
  29. this.isLoadingOne = true;
  30. setTimeout(() => {
  31. this.isLoadingOne = false;
  32. }, 5000);
  33. }
  34. loadTwo(): void {
  35. this.isLoadingTwo = true;
  36. setTimeout(() => {
  37. this.isLoadingTwo = false;
  38. }, 5000);
  39. }
  40. }

Button按钮 - 图4

按钮组合

可以将多个 nz-button 放入 nz-button-group 的容器中。

通过设置 nzSizelargesmall 分别把按钮组合设为大、小尺寸。若不设置 nzSize,则尺寸为中。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-button-group',
  4. template: `
  5. <h4>Basic</h4>
  6. <nz-button-group>
  7. <button nz-button>Cancel</button>
  8. <button nz-button nzType="primary">OK</button>
  9. </nz-button-group>
  10. <nz-button-group>
  11. <button nz-button nzType="default" disabled>L</button>
  12. <button nz-button nzType="default" disabled>M</button>
  13. <button nz-button nzType="default" disabled>R</button>
  14. </nz-button-group>
  15. <nz-button-group>
  16. <button nz-button nzType="primary" disabled>L</button>
  17. <button nz-button nzType="default" disabled>M</button>
  18. <button nz-button nzType="default">M</button>
  19. <button nz-button nzType="dashed" disabled>R</button>
  20. </nz-button-group>
  21. <h4>With Icon</h4>
  22. <nz-button-group>
  23. <button nz-button nzType="primary"><i nz-icon type="left"></i> Go back</button>
  24. <button nz-button nzType="primary">Go forward<i nz-icon type="right"></i></button>
  25. </nz-button-group>
  26. <nz-button-group>
  27. <button nz-button nzType="primary"><i nz-icon type="cloud"></i></button>
  28. <button nz-button nzType="primary"><i nz-icon type="cloud-download"></i></button>
  29. </nz-button-group>
  30. `,
  31. styles: [
  32. `
  33. h4 {
  34. margin: 16px 0;
  35. font-size: 14px;
  36. line-height: 1;
  37. font-weight: normal;
  38. }
  39. h4:first-child {
  40. margin-top: 0;
  41. }
  42. [nz-button] {
  43. margin-bottom: 12px;
  44. }
  45. nz-button-group {
  46. margin-bottom: 8px;
  47. margin-right: 8px;
  48. }
  49. `
  50. ]
  51. })
  52. export class NzDemoButtonButtonGroupComponent {}

Button按钮 - 图5

Block 按钮

nzBlock 属性将使按钮适合其父宽度。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-block',
  4. template: `
  5. <button nz-button nzType="primary" nzBlock>Primary</button>
  6. <button nz-button nzType="default" nzBlock>Default</button>
  7. <button nz-button nzType="dashed" nzBlock>Dashed</button>
  8. <button nz-button nzType="danger" nzBlock>Danger</button>
  9. `,
  10. styles: [
  11. `
  12. [nz-button] {
  13. margin-bottom: 12px;
  14. }
  15. `
  16. ]
  17. })
  18. export class NzDemoButtonBlockComponent {}

Button按钮 - 图6

图标按钮

当需要在 nz-button 内嵌入图标时,可以直接在 nz-button 内嵌入对应的 icon

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-icon',
  4. template: `
  5. <button nz-button nzType="primary" nzShape="circle"><i nz-icon type="search"></i></button>
  6. <button nz-button nzType="primary"><i nz-icon type="search"></i>Search</button>
  7. <button nz-button nzType="default" nzShape="circle"><i nz-icon type="search"></i></button>
  8. <button nz-button nzType="default"><i nz-icon type="search"></i>Search</button>
  9. <br />
  10. <button nz-button nzType="default" nzShape="circle"><i nz-icon type="search"></i></button>
  11. <button nz-button nzType="default"><i nz-icon type="search"></i>Search</button>
  12. <button nz-button nzType="dashed" nzShape="circle"><i nz-icon type="search"></i></button>
  13. <button nz-button nzType="dashed"><i nz-icon type="search"></i>Search</button>
  14. `,
  15. styles: [
  16. `
  17. [nz-button] {
  18. margin-right: 8px;
  19. margin-bottom: 12px;
  20. }
  21. `
  22. ]
  23. })
  24. export class NzDemoButtonIconComponent {}

Button按钮 - 图7

不可用状态

添加 disabled 属性即可让按钮处于不可用状态,同时按钮样式也会改变。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-disabled',
  4. template: `
  5. <button nz-button nzType="primary">Primary</button>
  6. <button nz-button nzType="primary" disabled>Primary(disabled)</button>
  7. <br />
  8. <button nz-button nzType="default">Default</button>
  9. <button nz-button nzType="default" disabled>Default(disabled)</button>
  10. <br />
  11. <button nz-button nzType="dashed">Dashed</button>
  12. <button nz-button nzType="dashed" disabled>Dashed(disabled)</button>
  13. <div style="padding: 8px 8px 0px; background: rgb(190, 200, 200);">
  14. <button nz-button nzGhost>Ghost</button>
  15. <button nz-button nzGhost disabled>Ghost(disabled)</button>
  16. </div>
  17. `,
  18. styles: [
  19. `
  20. [nz-button] {
  21. margin-right: 8px;
  22. margin-bottom: 12px;
  23. }
  24. `
  25. ]
  26. })
  27. export class NzDemoButtonDisabledComponent {}

Button按钮 - 图8

多个按钮组合

按钮组合使用时,推荐使用1个主操作 + n 个次操作,3个以上操作时把更多操作放到 nz-dropdown 中组合使用。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-multiple',
  4. template: `
  5. <button nz-button nzType="primary">primary</button>
  6. <button nz-button nzType="default">secondary</button>
  7. <nz-dropdown>
  8. <button nz-button nz-dropdown>Actions<i nz-icon type="down"></i></button>
  9. <ul nz-menu>
  10. <li nz-menu-item>
  11. <a>1st item</a>
  12. </li>
  13. <li nz-menu-item>
  14. <a>2nd item</a>
  15. </li>
  16. <li nz-menu-item>
  17. <a>3rd item</a>
  18. </li>
  19. </ul>
  20. </nz-dropdown>
  21. `,
  22. styles: [
  23. `
  24. [nz-button] {
  25. margin-right: 8px;
  26. margin-bottom: 12px;
  27. }
  28. `
  29. ]
  30. })
  31. export class NzDemoButtonMultipleComponent {}

Button按钮 - 图9

幽灵按钮

添加 nzGhost 属性后,幽灵按钮将其他按钮的内容反色,背景变为透明,常用在有色背景上。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-button-ghost',
  4. template: `
  5. <div style="background: rgb(190, 200, 200);padding: 26px 16px 16px;">
  6. <button nz-button nzType="primary" nzGhost>Primary</button>
  7. <button nz-button nzType="default" nzGhost>Default</button>
  8. <button nz-button nzType="dashed" nzGhost>Dashed</button>
  9. <button nz-button nzType="danger" nzGhost>Danger</button>
  10. </div>
  11. `,
  12. styles: [
  13. `
  14. [nz-button] {
  15. margin-right: 8px;
  16. margin-bottom: 12px;
  17. }
  18. `
  19. ]
  20. })
  21. export class NzDemoButtonGhostComponent {}

API

单独引入此组件

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

  1. import { NzButtonModule } from 'ng-zorro-antd';

[nz-button]directive

通过设置 Button 的属性来产生不同的按钮样式,推荐顺序为:nzType -> nzShape -> nzSize -> nzLoading -> disabled

按钮的属性说明如下:

属性说明类型默认值
[nzGhost]幽灵属性,使按钮背景透明booleanfalse
[nzLoading]设置按钮载入状态booleanfalse
[nzShape]设置按钮形状,可选值为 circleround 或者不设'circle'|'round'-
[nzSize]设置按钮大小,可选值为 smalllarge 或者不设'large'|'small'|'default''default'
[nzType]设置按钮类型,可选值为 primarydasheddanger 或者不设'primary'|'dashed'|'danger'|'default''default'
[nzBlock]将按钮宽度调整为其父宽度的选项booleanfalse