【问题标题】:change glyph-icon on click on the button in angular 2单击角度 2 中的按钮更改字形图标
【发布时间】:2017-04-13 10:18:29
【问题描述】:

我希望当我单击此按钮时,“glyphicon-plus”变为“glyphicon-minus”,当我再次单击时,它会再次“glyphicon-plus”。 我想要它应该是可切换的。我不想在 css 或 jquery 中使用它。 app.component.html:

<button data-toggle="collapse" 
        [attr.data-target]="'#demo' + RowIndex">
        <span class="glyphicon glyphicon-plus"></span>
</button>

【问题讨论】:

    标签: angular toggle glyphicons


    【解决方案1】:

    在您的模板中,使用sign 绑定和click 事件绑定,如下所示:

    <button data-toggle="collapse" 
            (click)="toggleSign()"
            [attr.data-target]="'#demo' + RowIndex">
            <span class="glyphicon glyphicon-{{sign}}"></span>
    </button>
    

    在您的组件类中,每次单击时更改sign 属性。

    export class MyComponent {
       ....
       sign = 'plus';  // or minus if you want that first
    
       toggleSign() {
         if(this.sign == 'plus') {
           sign = 'minus';
         } else {
           sign = 'plus';
         }
       }
      ......
    
     }
    

    在 Angular 中总是这样想——我的观点是我的组件状态的反映。因此,如果我想在视觉上进行更改,请使用绑定系统将视图的可变部分绑定到组件的属性,然后更改组件属性以反映我想要的视图。

    【讨论】:

    • 伟大的 snorkpete... 这对我有用。谢谢:)
    • 我还有一个问题。你能调查一下吗?我在这里提供链接:stackoverflow.com/questions/43391568/…。谢谢
    • @snorkpete 这是一个很好的解决方案。但是当我快速点击时,图标变化太快 - 不等待打开/关闭折叠字段。你知道如何预防吗?
    • @snorpete 如果有多个按钮怎么办
    猜你喜欢
    • 2017-01-14
    • 2019-01-01
    • 1970-01-01
    • 2017-10-14
    • 2014-02-16
    • 2019-03-28
    • 1970-01-01
    • 2018-06-07
    • 2023-02-07
    相关资源
    最近更新 更多