【问题标题】:How to make specific <div> show if checkbox with specific name is checked off?如果选中具有特定名称的复选框,如何使特定的 <div> 显示?
【发布时间】:2020-10-19 14:40:42
【问题描述】:

我有一个 Angular Material Stepper 元素,第一个 &lt;mat-step&gt; 包含一个复选框列表供用户检查。

以下是第一个带有复选框的&lt;mat-step&gt;

<mat-step [stepControl]="itemCheckboxes">
  <ng-template matStepLabel>Select The Items You Want</ng-template>
  <span *ngIf="items.length === 0">No items currently in your account.</span>
  <ul style="list-style-type:none; padding-left: 0px;">
    <li *ngFor="let item of items">
      <mat-checkbox (change)="onCheckboxChange($event, item.name)">{{ item.name }}</mat-checkbox>
    </li>
  </ul>
  <div>
    <button mat-button matStepperNext>Next</button>
  </div>
</mat-step>

我正在尝试在以下&lt;mat-step&gt; 中显示特定的&lt;div&gt;,前提是选中具有特定{{item.name}}s 的复选框。

例如,如果用户选中一个名为“Banana”的复选框,则与选中的“Banana”复选框相关的特定 &lt;div&gt; 将显示。如果用户选中一个名为“Apple”的复选框,则会显示不同的&lt;div&gt;

我将如何编写代码来实现这一目标?我正在尝试编辑现有项目,而且我对编码非常陌生,但我需要快速完成这项工作,因此非常感谢分步详细信息。谢谢!

【问题讨论】:

    标签: javascript angular angular-material


    【解决方案1】:

    您需要了解“Angular 哲学”。 .ts(模型)中的变量与 html(视图)中的元素角度相关

    所以如果你声明一个变量

    myvariable:boolean=true
    

    你的复选框是

     <mat-checkbox [(ngModel)]="myvariable">Banana</mat-checkbox>
    

    两个方向的 [(ngModel)] 关系-绑定两种方式-变量和复选框。如果选中复选框,则变量为 true,如果取消选中复选框,则变量为 false。

    所以你可以在任何地方写作

    <div *ngIf="myvariable">
       If you see this is because the checkbox is true
    </div>
    

    你有一个“项目”数组,所以如果你做一个绑定 - 使用 [(ngModel)] 到一个变量

     <mat-checkbox [(ngModel)]="item.checked">{{ item.name }}</mat-checkbox>
    

    你可以写

    <div *ngIf="items[0].checked">
       You check the first element of the list before
    </div>
    

    更新如果我们想知道“香蕉”是否被选中

    好吧,我想我们有一个对象数组

    items=[{name:"Banana"},{name:"apple"},{name:"orange"}...]
    

    检查一个或多个时,我们将阵列转换为某些

    中的数组
    items=[{name:"Banana",checked:true},{name:"apple"},{name:"Orange",checked:false}...]
    

    查看一个或多个元素可以没有“已检查”属性

    类似的函数

    isSelect(fruit:string)
    {
        return this.items.find(x=>x.checked && x.name==fruit)!=null
    }
    

    返回真或假。现在您有多种选择:

    1.-使用吸气剂

    get isBananaSelected()
    {
        return this.isSelect("Banana")
    }
    

    并在 ngIf 中使用

       <div *ngIf="isBananaSelected">...</div>
    

    2.-使用一个辅助变量,当步进器改变步长时计算

    <mat-horizontal-stepper (selectionChange)="selectionChange($event)">
    
    isVisibleDiv:boolean=false //and auxiliar variable
    selectionChange(event)
    {
      if (event.selectedIndex==1){ //remember the index is 0 for the first step, 1 for the second...
          this.isVisibleDiv=this.isSelect("Banana")
      }
    }
    
    //and 
    <div *ngIf="isVisibleDiv">...</div>
    

    3.-使用辅助变量并在每次选择中调用函数

    <mat-checkbox (change)="isVisibleDiv=this.isSelect("Banana")">
    

    注意:看起来 getter 是最好的选择,但要小心,getter 会执行多次,第二个选项仅在更改步骤时执行,第三个选项在任何复选框中执行每次更改。 (实际上对于单纯形函数——就像在这种情况下——你没有注意到更好或最差的性能),但我们需要在简单和性能之间进行选择

    【讨论】:

    • 谢谢!但是,使用此解决方案,
      将根据列表的第一个元素显示。我知道您可以更改
      中的数字以调整列表中的哪个元素作为其基础,但是您将如何根据名称显示它?因为我希望一个特定的
      仅在选中“香蕉”时才显示,无论它在列表中的哪个位置。
    • 我更新答案显示三种方法,你需要选择其中一种
    【解决方案2】:

    您可以使用ng-templatengTemplateOutlet。这是DEMO。该示例仅显示模板的显示。取消选中复选框时,可以从数组中删除模板引用

    import {
      Component,
      TemplateRef,
      VERSION,
      ViewChild
    } from "@angular/core";
    
    @Component({
      selector: "my-app",
      templateUrl: "./app.component.html",
      styleUrls: ["./app.component.css"]
    })
    export class AppComponent {
      templates = [];
      items = [{
          name: "Banana"
        },
        {
          name: "Apple"
        },
        {
          name: "Orange"
        },
        {
          name: "Brinjal"
        }
      ];
      @ViewChild("Banana") Banana: TemplateRef < any > ;
      @ViewChild("Apple") Apple: TemplateRef < any > ;
      @ViewChild("Orange") Orange: TemplateRef < any > ;
      @ViewChild("Brinjal") Brinjal: TemplateRef < any > ;
      onCheckboxChange(event, name) {
        this.templates.push(this[name]);
      }
    }
    
    <ng-template matStepLabel>Select The Items You Want</ng-template>
    <span *ngIf="items.length === 0">No items currently in your account.</span>
    <ul style="list-style-type:none; padding-left: 0px;">
      <li *ngFor="let item of items">
        <mat-checkbox (change)="onCheckboxChange($event, item.name)">{{ item.name }}</mat-checkbox>
      </li>
    </ul>
    <div>
      <button mat-button matStepperNext>Next</button>
    </div>
    
    <div *ngFor="let tmpl of templates">
      <ng-container [ngTemplateOutlet]="tmpl"></ng-container>
    </div>
    
    
    <ng-template #Banana>
      <span>Banana</span></ng-template>
    
    <ng-template #Apple>
      <span>Apple</span></ng-template>
    
    <ng-template #Orange>
      <span>Orange</span></ng-template>
    
    <ng-template #Brinjal>
      <span>Brinjal</span></ng-template>
    

    【讨论】:

    • 谢谢@brk,但由于某种原因,当我在我的应用程序中使用代码时,这不起作用。我认为这是因为我已经为与您提供的复选框相矛盾的复选框提供了“onCheckboxChange($event, item.name)”功能?另外,“@ViewChild”函数中的“Yellow Banana”超过 1 个单词,我该如何写?
    • 试试这样YellowBanana
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签