【问题标题】:Angular - How to bind a button for different uses?Angular - 如何绑定不同用途的按钮?
【发布时间】:2019-07-21 06:19:45
【问题描述】:

我是 Angular 和 TypeScript 的新手。

我必须做一个可以有文本或图标或文本+图标的按钮。

例子:

button-icon-text-component.html

<button>
  TEST BUTTON
</button>

app.component.html

<app-button-icon-text {here I have to choose beetwen TEXT or ICON}></app-button-icon-text>

button-icon-text-component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-button-icon-text',
  templateUrl: './button-icon-text.component.html',
  styleUrls: ['./button-icon-text.component.css']
})
export class ButtonIconTextComponent implements OnInit {

{ADD SOME LOGIC}

  constructor() { }

  ngOnInit() {
  }

}

【问题讨论】:

    标签: html angular typescript components frontend


    【解决方案1】:

    这些是我的 3 个例子:

    对于我安装的图标:npm i material-icons

    然后我在 style.css 中导入材质:@import '~material-icons/iconfont/material-icons.css';

    app.component.html

    //text button
    <app-button-icon-text [text]="'test'"></app-button-icon-text>
      <br><br>
    
    //iconbutton
      <app-button-icon-text [icon]="'face'"></app-button-icon-text>
      <br><br>
    
    //icon + text button
      <app-button-icon-text [text]="'test'" [icon]="'face'"></app-button-icon-text>
    

    button-icon-text.component.html

    <button>
      <span *ngIf="icon"  class="material-icons">{{icon}}</span>
      {{text}}
    </button>
    

    button-icon-text.component.html

    import {Component, Input, OnInit} from '@angular/core';
    
    @Component({
      selector: 'app-button-icon-text',
      templateUrl: './button-icon-text.component.html',
      styleUrls: ['./button-icon-text.component.css']
    })
    export class ButtonIconTextComponent implements OnInit {
    
      @Input()
      text;
    
      @Input()
      icon;
    
      constructor() {
      }
    
      ngOnInit() {
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      使用@Input装饰器并在其上设置'TEXT''ICON'

      stackblitz:https://stackblitz.com/edit/angular-qmnp2u

      app.component.html:

      <app-button-icon-text type="TEXT"></app-button-icon-text>
      

      button-icon-text-component.ts:

      import { Component, OnInit, Input } from '@angular/core';
      
      @Component({
        selector: 'app-button-icon-text',
        templateUrl: './button-icon-text.component.html',
        styleUrls: ['./button-icon-text.component.css']
      })
      export class ButtonIconTextComponent implements OnInit {
      
        @Input()
        type // TEXT or ICON
      
        constructor() { }
      
        ngOnInit() {
        }
      
      }
      

      button-icon-text-component.html:

      <button *ngIf="type === 'TEXT'">
        TEST BUTTON
      </button>
      ...
      

      【讨论】:

      • 嗨@zmag,谢谢。我的问题是,我不知道如何区分 Text 类型和 Icon 类型。所以我不知道在输入或ngOnInit中插入什么。你能更完整吗?
      • 试试 *ngIf 语法。我制作了 stackblitz 示例并更新了我的帖子。 stackblitz.com/edit/angular-qmnp2u
      • 好的,谢谢!我使用您的想法为我的项目定制了它!祝你有美好的一天;)
      猜你喜欢
      • 2018-12-08
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2011-10-04
      • 2021-06-04
      相关资源
      最近更新 更多