【问题标题】:toggle among three icons in html upon click单击时在 html 中的三个图标之间切换
【发布时间】:2018-08-07 19:06:57
【问题描述】:

我有一个按钮,每次点击都会改变图标。该按钮有下一个箭头、后退箭头、停止图标。处理按钮的最佳方式是什么,所以每次点击图标都会改变。

HTML:

 <button (click)="toggleBtn()">
      <span *ngIf="selectedIcon='left'" class="icon-left"></span>
      <span *ngIf="selectedIcon='right'" class="icon-right"></span>
      <span *ngIf="selectedIcon='stop'" class="icon-stop"></span>
    </button>

组件:

 public ngOnInit() {
        this.selectedIcon="Both"; //default selection to display to user.
    }

 public toggleBtn(){
   // how should i handle the toggle? User a for loop in my component or switch? 
   // what would be the ideal way of achieving this. 
 }

【问题讨论】:

  • selectedIcon"Both" 时应该出现什么?
  • @ConnorsFan - 只不过是使用所选图标启用特定跨度。在页面加载时它会显示停止图标,点击它会显示左图标,再次点击它会显示右图标。我是否应该在我的组件中有一个 for 循环并只传递所选切换的图标类?

标签: javascript html angular


【解决方案1】:

使用余数运算符。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()

//in your .ts declare a variable index
index:number=0;

//in your .html
<button (click)="index=(index+1)%3">
<span [ngClass]="{'icon-left':index==0,'icon-right':index==1,'icon-stop':index==2}">

更新 如果您有多个项目,则需要多个变量。通常你可以有一个对象数组 [{item:'uno',index:0}{item:'dos',index:0}..]。在 *ngFor="let item of myArray"> 中,将“index”替换为“item.index”

//In your .ts
myArray:any[]=[{desc:'uno',index:0},{desc:'dos',index:0}..];

//If you have an array of object but you don't have the property "index"
//You always can "map" the array to add the property
//e.g
anotherArray=["uno","dos"];

//transform the array
this.anotherArray=thia.anotherArray.map(x=>return{
       desc:x,
       index:0
})

<button *ngFor="let item of my array" (click)="item.index=(item.index+1)%3">
   <span [ngClass]="{
            'icon-left':item.index==0,
            'icon-right':item.index==1,
            'icon-stop':item.index==2}">{{item.desc}}
</button>

更新两个 我们可以有不同的按钮,但不能在 *ngFor 中,为此,使用索引数组

//in ts
index:number[]=[0,0]
//in .html
<!--for the first button we use index[0]-->
<button (click)="index[0]=(index[0]+1)%3">
   <span [ngClass]="{
            'icon-left':index[0]==0,
            'icon-right':index[0]==1,
            'icon-stop':index[0]==2}">
</button>
<hr/>
<!--for the second button we use index[1]-->
<button (click)="index[1]=(index[1]+1)%3">
   <span [ngClass]="{
            'icon-left':index[1]==0,
            'icon-right':index[1]==1,
            'icon-stop':index[1]==2}">
</button>
<hr/>

【讨论】:

  • 不错的答案!我刚刚尝试过,它按预期工作。另外,如果我要保存图标选择.. 获取组件中的当前索引并在我的保存中使用它就足够了吗?
  • 我注意到,如果我有多个项目,每个项目都有切换按钮。切换适用于所有项目,而不是我单击的特定项目。
  • @bluePearl。更新我的答案,你需要几个“索引”。您可以使用数字数组或将属性“索引”添加到列表中
  • 我继续尝试您更新的答案,但现在我为每个列表项显示所有三个图标,并且三个图标中的每一个都是可点击和切换的......不确定我是否忽略了某些东西但什么是this.anotherArray 做什么?
  • 这里是 stackbiltz 链接,看看我在说什么......stackblitz.com/edit/angular-mqcc8j
【解决方案2】:

可以使用ngClass属性绑定。

<button (click)="toggleBtn(selectedIcon)" [ngClass]="[selectedIcon=='left' ? 'icon-left' : '',  selectedIcon=='right' ? 'icon-right' : '', selectedIcon=='stop' ? 'icon-stop' : '']">

内部组件:

selectedIcon;

toggleBtn(_sIcon){
   if(!_sIcon){
     this.selectedIcon = 'left';
   }else if(_sIcon === 'left){
     this.selectedIcon = 'right';
   }else if(_sIcon === 'right){
     this.selectedIcon = 'stop';
   }else if(_sIcon === 'stop){
     this.selectedIcon = 'left';
   }
}

【讨论】:

  • 不要使用大的“if”链。获得值 0,1,2,0,1,2,0,1,2... 的变量就足够了。所以使用 % 运算符
  • 好的。没关系。你也可以使用 switch case 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多