Button按钮

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

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

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

    1. @Component({
    2. selector: 'nz-demo-button-basic',
    3. template: `
    4. <button nz-button nzType="primary">Primary</button>
    5. <button nz-button nzType="default">Default</button>
    6. <button nz-button nzType="dashed">Dashed</button>
    7. <button nz-button nzType="danger">Danger</button>
    8. <button nz-button nzType="link">Link</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. <button nz-button [nzSize]="size" nzType="link">Link</button>
    17. <br />
    18. <button nz-button nzType="primary" [nzSize]="size" nzShape="circle"><i nz-icon nzType="download"></i></button>
    19. <button nz-button nzType="primary" [nzSize]="size" nzShape="round">
    20. <i nz-icon nzType="download"></i>Download
    21. </button>
    22. <button nz-button nzType="primary" [nzSize]="size"><i nz-icon nzType="download"></i>Download</button>
    23. <br />
    24. <nz-button-group [nzSize]="size">
    25. <button nz-button nzType="primary"><i nz-icon nzType="left"></i>Backward</button>
    26. <button nz-button nzType="primary">Forward<i nz-icon nzType="right"></i></button>
    27. </nz-button-group>
    28. `,
    29. styles: [
    30. `
    31. [nz-button] {
    32. margin-right: 8px;
    33. margin-bottom: 12px;
    34. }
    35. nz-button-group [nz-button] {
    36. margin-right: 0;
    37. }
    38. `
    39. ]
    40. })
    41. export class NzDemoButtonSizeComponent {
    42. }

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

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

    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. <button nz-button nzType="link" nzBlock>Link</button>
    10. `,
    11. styles: [
    12. [nz-button] {
    13. margin-bottom: 12px;
    14. }
    15. `
    16. ]
    17. })
    18. export class NzDemoButtonBlockComponent {}

    Button按钮 - 图6

    图标按钮

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

    添加 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. <button nz-button nzType="dashed">Dashed</button>
    11. <button nz-button nzType="dashed" disabled>Dashed(disabled)</button>
    12. <br />
    13. <button nz-button nzType="link">Link</button>
    14. <button nz-button nzType="link" disabled>Link(disabled)</button>
    15. <div style="padding: 8px 8px 0px; background: rgb(190, 200, 200);">
    16. <button nz-button nzGhost>Ghost</button>
    17. <button nz-button nzGhost disabled>Ghost(disabled)</button>
    18. </div>
    19. `,
    20. styles: [
    21. `
    22. [nz-button] {
    23. margin-right: 8px;
    24. margin-bottom: 12px;
    25. }
    26. `
    27. ]
    28. })
    29. 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. <button nz-button nz-dropdown [nzDropdownMenu]="menu">Actions<i nz-icon nzType="down"></i></button>
    8. <nz-dropdown-menu #menu="nzDropdownMenu">
    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-menu>
    21. `,
    22. styles: [
    23. `
    24. [nz-button] {
    25. margin-right: 8px;
    26. margin-bottom: 12px;
    27. }
    28. `
    29. ]
    30. })

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

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