【问题标题】:Mutually exclusive buttons in angular 6Angular 6 中的互斥按钮
【发布时间】:2019-03-05 08:09:15
【问题描述】:

我在 ngFor 组件中有一组按钮,我希望它们是互斥的。我想设计它,当我单击一行中的第一个按钮时,在我单击同一行中的相应按钮之前不能再次单击它。这是我的 Typescript sn-p 和我创建的函数;

showTime(staff){
        staff.time = new Date();
     }
     displayTime(staff){

        staff.times = new Date();
    }    

这是我的 HTML sn-p;

    <tbody>
               <tr *ngFor="let staff of staffs" class="m-1 p-1 bg-light">
                    <td>
                        {{staff.name}}  
                    </td>
                    <td>
                            {{staff.department}}
                    </td>
                    <td>
                        <button [disabled]="!flagOne" (click)="showTime(staff)">Click</button>
                        <span *ngIf="staff.time">
                            Time: {{staff.time | date: 'shortTime'}}
                        </span>
                    </td>
                    <td>
                        <button [disabled]="!flagTwo" (click)="displayTime(staff)">Click</button>
                        <span *ngIf="staff.times">
                            Time: {{staff.times | date: 'shortTime'}}
                        </span>
                    </td>
                </tr>
           </tbody>

我的数组如下所示:

module.exports = function () {
  return {
    staff: [
        { id: 1, name: "xxx", department: "Development",
           role: "Team Lead", time: null },
        { id: 2, name: "xxx", department: "Development",
           role: "Back End", time: null },
        { id: 3, name: "xxx", department: "Security",
           role: "Front End",time: null },
        { id: 4, name: "xxx", department: "Infrastructure",
           role: "Corper", time: null },
        { id: 5, name: "xxx", department: "Infrastructure",
           role: "xxx", time: null },
           { id: 6, name: "xxx", department: "Management",
           role: "Assistant", time: null },
           { id: 7, name: "xxx", department: "Management",
           role: "Director", time: null }
                ],
    entries: []
}

【问题讨论】:

    标签: html angular typescript


    【解决方案1】:

    在你的组件中创建两个标志,用这些标志控制按钮的disabled 属性。在调用showTime()displayTime() 方法时设置它们。

    .ts

     export class AppComponent  {
      name = 'Angular';
      staff = {time: null};
      flagOne = true;
      flagTwo = true;
      showTime(staff){
        this.staff.time = new Date();
        this.flagOne = false;
        this.flagTwo = true;
      }
    
      displayTime(staff){
        staff.times = new Date();
        this.flagTwo = false;
        this.flagOne = true;
      }    
    }
    

    .html

     <td>
          <button [disabled]="!flagOne" (click)="showTime(staff)">Button One!</button>
          <span *ngIf="staff.time">
              Time: {{staff.time | date: 'shortTime'}}
          </span>
      </td>
      <td>
          <button [disabled]="!flagTwo" (click)="displayTime(staff)">Button Two!</button>
          <span *ngIf="staff.times">
              Time: {{staff.times | date: 'shortTime'}}
          </span>
      </td>
    

    https://stackblitz.com/edit/angular-jwahcd?file=src%2Fapp%2Fapp.component.ts

    【讨论】:

    • 成功了。但我不想禁用表格列中的所有按钮。只是我点击的那个
    • @EmekeObuseh:请添加完整的代码,因为据我所知,您的问题中只有两个按钮。避免问 XY 问题,例如:xyproblem.info
    • @xyz,您可以使用唯一的“标志”来简单地编写代码。好吧,如果您使用变量“buttonClicked”,&lt;button (click)="buttonClicked=1" [disabled]="buttonClicked==1"&gt;click&lt;/button&gt;
    猜你喜欢
    • 2018-02-04
    • 2011-09-24
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多