首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选中“角-显示组件”复选框。每个复选框都有单独的组件。

选中“角-显示组件”复选框。每个复选框都有单独的组件。
EN

Stack Overflow用户
提问于 2020-08-14 16:16:19
回答 2查看 455关注 0票数 0

假设我有4个复选框。

代码语言:javascript
复制
<mat-checkbox>1</mat-checkbox>
<component-1></component-1>
<mat-checkbox>2</mat-checkbox>
<component-2></component-2>
<mat-checkbox>3</mat-checkbox>
<component-3></component-3>
<mat-checkbox>4</mat-checkbox>
<component-4></component-4>

每个mat-checkbox都有自己的component。当用户选中前3个复选框中的任何一个时,它将显示/加载组件,但如果用户选中了第4个mat-checkbox,它将取消选中上述三个mat-checkbox,从而导致组件隐藏,只显示第4个组件。

CASE1:-用户可以一起检查前三个中的任何一个。

CASE2 2:-用户可以检查前三名或第四名。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-14 17:29:10

实例化

如果我无意中遗漏了什么,请告诉我。

我是这样做到的:

主应用程序模板:

代码语言:javascript
复制
    <hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>

<mat-checkbox #checkOne (change)="OnChkChanged(1)">1</mat-checkbox>
<component-1 *ngIf="showOne"></component-1>

<mat-checkbox #checkTwo (change)="OnChkChanged(2)">2</mat-checkbox>
<component-2 *ngIf="showTwo"></component-2>

<mat-checkbox #checkThree (change)="OnChkChanged(3)">3</mat-checkbox>
<component-3 *ngIf="showThree"></component-3>

<mat-checkbox #checkFour (change)="OnChkChanged(4)">4</mat-checkbox>
<component-4 *ngIf="showFour"></component-4>

主应用程序组件:

代码语言:javascript
复制
import { Component, VERSION, ViewChild, ElementRef } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular " + VERSION.major;
  @ViewChild("checkOne") checkOne: ElementRef;
  @ViewChild("checkTwo") checkTwo: ElementRef;
  @ViewChild("checkThree") checkThree: ElementRef;
  @ViewChild("checkFour") checkFour: ElementRef;

  showOne = false;
  showTwo = false;
  showThree = false;
  showFour = false;

  OnChkChanged(id: number) {
    if (id === 1) {
      this.showOne = !this.showOne;
    }
    if (id === 2) {
      this.showTwo = !this.showTwo;
    }
    if (id === 3) {
      this.showThree = !this.showThree;
    }
    if (id === 4) {
      this.showFour = !this.showFour;
      if (this.showFour === true) {
        this.uncheckAllThree();
      }
    }
  }

  uncheckAllThree() {
    this.checkOne["checked"] = false;
    this.showOne = false;
    this.checkTwo["checked"] = false;
    this.showTwo = false;
    this.checkThree["checked"] = false;
    this.showThree = false;
  }
}

ComponentOne:

代码语言:javascript
复制
import { Component } from '@angular/core';

@Component({
  selector: 'component-1',
  template: '<h3>I am Component 1</h3>',
})
export class ComponentOne  {
  
}

ComponentTwo:

代码语言:javascript
复制
import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'component-2',
  template: '<h3>I am Component 2</h3>',
})
export class ComponentTwo  {
  
}

ComponentThree:

代码语言:javascript
复制
import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'component-3',
  template: '<h3>I am Component 3</h3>',
})
export class ComponentThree  {
  
}

ComponentFour:

代码语言:javascript
复制
import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'component-4',
  template: '<h3>I am Component 4</h3>',
})
export class ComponentFour  {
  
}
票数 1
EN

Stack Overflow用户

发布于 2020-08-14 16:42:37

我从问题中了解到,前三个复选框对第四个复选框没有影响,但是第四个复选框对上述三个复选框都有影响。

解决方案:

单击第四个复选框时,必须使用@Output()将事件传递给父组件,然后可以在包含其他复选框的三个独立组件中使用属性或调用方法。

您必须更改属性,以防您正在处理来自父组件的数据,并且您必须调用方法,以防您处理每个组件中的数据。

如果你不明白我说的话,那么如果你能向我们展示你在https://stackblitz.com/中的工作,那就太好了。

可能有多种解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63416446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档