简体   繁体   English

如何在角度绑定样式

[英]How to bind style in angular

i am trying to highlighted selected item from given items using below style but its highlighting all items when i tap on items but i want to highlighted only selected item can some one help me please https://stackblitz.com/edit/angular-7wkrx1?file=src/app/app.component.html 我正在尝试使用以下样式突出显示给定项目中的选定项目,但是当我点击项目时突出显示所有项目,但是我想仅突出显示选定项目可以帮助我一点https://stackblitz.com/edit/angular-7wkrx1 ?文件= SRC /应用/ app.component.html

.css 的CSS

/* Style the tab */
.tab {
  float: left;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
  width: 30%;
  height: 300px;
}

/* Style the buttons that are used to open the tab content */
.tab button {
  display: block;
  background-color: inherit;
  color: black;
  padding: 22px 16px;
  width: 100%;
  border: none;
  outline: none;
  text-align: left;
  cursor: pointer;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current "tab button" class */
.tab button.active {
  background-color: red;
}

.html html的

<div class="tab">
  <button class="tablinks" [class.active]="one" (click)="one=!one">London</button>
  <button class="tablinks" [class.active]="two" (click)="two=!two">Paris</button>
  <button class="tablinks" [class.active]="three" (click)="three=!three">Tokyo</button>
</div> 

One solution would be to use one variable to store the selected option: 一种解决方案是使用一个变量存储所选选项:

<div class="tab">
  <button class="tablinks" [class.active]="active == 'one'" (click)="active = 'one'">London</button>
  <button class="tablinks" [class.active]="active == 'two'" (click)="active = 'two'">Paris</button>
  <button class="tablinks" [class.active]="active == 'three'" (click)="active = 'three'">Tokyo</button>
</div> 

Instead, use ngClass , this would be better approach in case of dynamic Class binding. 而是使用ngClass ,这在动态类绑定的情况下会更好。

<div class="tab">
  <button class="tablinks" [ngClass]='{"active": selected == "one"}' (click)="selected = 'one'">London</button>
  <button class="tablinks" [ngClass]='{"active": selected == "two"}' (click)="selected = 'two'">Paris</button>
  <button class="tablinks" [ngClass]='{"active": selected == "three"}' (click)="selected = 'three'">Tokyo</button>
</div> 

Working Example 工作实例

PS: If you have more options like this, you should go with ngSwitchCase of angular, that could be an efficient one. PS:如果您有更多这样的选择,则应使用ngSwitchCase angular,这可能是一个有效的选择。

Another way, you can do it by typescript logic, 换句话说,您可以通过打字稿逻辑来实现,

HTML: HTML:

<div class="tab">
  <button class="tablinks" [class.active]="one" (click)="function1()">London</button>
  <button class="tablinks" [class.active]="two" (click)="function2()">Paris</button>
  <button class="tablinks" [class.active]="three" (click)="function3()">Tokyo</button>
</div> 

.TS .TS

  function1(){
    this.one=!this.one;
    this.two= this.three = false;
  }

   function2(){
    this.two=!this.two;
    this.one= this.three = false;
  }

   function3(){
    this.three=!this.three;
    this.one = this.two = false;
  }

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

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