简体   繁体   English

如何在Angular 6中更改按钮状态?

[英]How do I change Button States in Angular 6?

I am new to Angular 6, my website contains a section which displays a set of buttons formed dynamically as follows 我是Angular 6的新手,我的网站包含一个部分,其中显示一组动态形成的按钮,如下所示

<button class="card-button" *ngFor="let env of product.env" name="button">{{env.name}}</button>

Also, I want to display different border of a button which is selected by default as 另外,我想显示按钮的不同边框,默认情况下该按钮被选中为

button:focus {
      border-bottom: 4px solid #CC292B;
      color: #CC292B;
}
button{
      border-bottom: 4px solid green;
      color: green;
}

Now If the button is selected it should come up with color: #CC292B; 现在,如果选择了按钮,它应该带有color: #CC292B; and green otherwise. 否则为绿色。 Through the above CSS, I was able to achieve the task. 通过上面的CSS,我能够完成任务。 Now I wanted the first button to be selected by default ie the first should appear with color: #CC292B; 现在,我希望默认情况下选择第一个按钮,即第一个按钮应显示为以下color: #CC292B; and rest with green color, how can I achieve this? 靠绿色休息,我该如何实现? Thanks in advance 提前致谢

Use ngClass to achieve that: 使用ngClass实现:

<button class="card-button" *ngFor="let env of product.env; let i = index" [ngClass]="[i == 0 ? 'button-active' : 'button-standard']" name="button">{{env.name}}</button>

and you can have this kind of css 你可以有这种CSS

.button-active{
color:red;
}

.button-standard{
color: green;
}

Use [style.color] to index=0 使用[style.color] index=0

[style.color]="{'#CC292B' : i == 0}"

See code: 看到代码:

<button class="card-button" *ngFor="let env of product.env;let i=index;" name="button" [style.color]="{'#CC292B' : i == 0}">{{env.name}}</button>

BTW you can do only with css with :first-child 顺便说一句,您只能使用带有:first-child CSS来做

 .card-button:first-child { color: red; } 
 //like ngFor <button class="card-button" name="button">{{env.name}}</button> <button class="card-button" name="button">{{env.name}}</button> <button class="card-button" name="button">{{env.name}}</button> 

Create two class for selected and notselected . selectednotselected创建两个class Apply class selected if it is first button, otherwise notselected 如果是第一个按钮,则应用selected类别,否则notselected

<button class="card-button" *ngFor="let env of product.env; let idx = index" 
[ngClass]="[idx == 0 ? 'selected' : 'notselected']" name="button">{{env.name}}</button>

Or you can color the button as below: 或者,您可以按如下所示为按钮着色:

<button class="card-button" *ngFor="let env of product.env; let idx = index" 
[style.color]="{'green' : idx != 0, '#CC292B': idx == 0}" name="button">{{env.name}}</button>

If you want to change the button style dynamically on user click use this: 如果要根据用户动态更改按钮样式,请使用以下命令:

In your component add this method: 在您的组件中添加以下方法:

public isselected=0;
buttonClick(index){
    this.isselected=index;
}

In HTML add the below code: 在HTML中添加以下代码:

   <div *ngFor="let env of carsList; let i =index">
        <button (click)="buttonClick(i)" class="card-button" name="button" [ngClass]="[i == isselected ? 'button-active' : 'button-standard']" >{{env}}</button>
        </div>

Use [ngClass] to add a class to the button that should get the focus automatically after the view is initialized. 使用[ngClass]将一个类添加到按钮,该类应在初始化视图后自动获得焦点。

<button 
    class="card-button" 
    *ngFor="let env of product.env; let i = index" 
    [ngClass]="[i == 0 ? 'autoFocusMe' : '']" 
    name="button">
    {{env.name}}
</button>

Then add method ngAfterViewInit() to your component.ts file to set the focus to that button 然后将ngAfterViewInit()方法添加到component.ts文件中,将焦点设置为该按钮

ngAfterViewInit() {
    (<HTMLButtonElement>document.getElementsByClassName("autoFocusMe")[0]).focus();
}

I have created working example .Please check 我已经创建了工作示例。请检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM