简体   繁体   English

Angular:如何在子组件之间切换选择

[英]Angular : how to toggle selection between children components

So I have got a parent component with children components binded over an ngFor() such as:所以我有一个父组件,其子组件绑定在ngFor()例如:

<categories *ngFor= "let category of categories" [category]="category"></categories>

Each of this categories has a title text with an ngClass like this one (being selected a variable I set to true in the children component when I click on it):每个类别都有一个带有ngClass的标题文本,就像这样(当我点击它时,我在子组件中选择了一个我设置为 true 的变量):

        <h3 class="my-3 px-1 category" [ngClass]="{'category-selected':selected}">
            {{ category.Title }} 
        </h3>

What I have been trying to do with little success is to turn this text green when you click on the category children element.我一直试图做的但收效甚微的是,当您单击类别子元素时,将此文本变为绿色。

That works fine but my main issue is that whenever I click a second category after the first one, the first one still has the text set to green and I dont know how to set this text back to its original color when you click on a different category (ie reset its color if the category is not selected).这工作正常,但我的主要问题是,每当我在第一个类别之后单击第二个类别时,第一个类别的文本仍然设置为绿色,我不知道当您单击其他类别时如何将此文本设置回其原始颜色类别(即如果未选择类别,则重置其颜色)。

This can be done by emiiting an event from category and listening to it from parent.这可以通过从类别发出一个事件并从父级监听它来完成。

Something like this.像这样的东西。

Live example活生生的例子

in component.ts在 component.ts

selectedCategory
onSelect(category){
this.selectedCategory=category
}

in template:在模板中:

<categories (selected)="onSelect(category)"
      [class.category-selected]="selectedCategory===category"
 *ngFor = "let category of categories" [category]="category"></categories>

Solution解决方案

So, after some testing I finally figured out the way to solve this.所以,经过一些测试,我终于找到了解决这个问题的方法。

In the parent component that contains the ngFor you pass a variable that will keep the information on which category is being selected :在包含ngFor的父组件中,您传递一个变量,该变量将保留有关正在选择哪个类别的信息:

<categories (click)="selection(category)"   *ngFor= "let category of categories"  [category]="category"  [selectedCategory]="selectedCategory"></categories>

So, whenever you click any of these categories, the selection variable will get updated such as:因此,无论何时单击这些类别中的任何一个,选择变量都会更新,例如:

  selection(category){
      this.selectedCategory = category.Title; 
  }

Then, in the children components you get this parent variable using: @Input() selectedCategory:string;然后,在子组件中,您使用以下@Input() selectedCategory:string;获取此父变量: @Input() selectedCategory:string; and then you use a trigger that will detect whenever the parent variable has changed:然后你使用一个触发器来检测父变量何时发生变化:

  ngOnChanges(changes: SimpleChanges) {    
    this.setClass(changes.selectedCategory.currentValue);    
}

And finally check if the newly selected variable is the same as the one in the component.最后检查新选择的变量是否与组件中的变量相同。 I have created a variable selected that will be true if that component is the one selected an false if is the component that has not being selected.我创建了一个 selected 变量,如果该组件是被选中的组件,则该变量将为 true,如果该组件未被选中,则该变量为 false。 This will allow me to reset the class of the component back to its original class.这将允许我将组件的类重置回其原始类。

setClass(value){
  if(value===this.category.Title){
    this.selected = true;
  }else{
    this.selected = false;
  }
}

And to finish, I set my desired component to an ngClass that depends on this selected variable.最后,我将我想要的组件设置为一个 ngClass,它取决于这个选定的变量。

<h4 [ngClass]="{'category-selected': selected, 'category-unselected': !selected }">Text</h4>

Hope you understood everything correctly if you encounter the same issue.如果您遇到同样的问题,希望您能正确理解所有内容。 Don't hesitate in asking if you have any questions.如果您有任何问题,请不要犹豫。 :D :D

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

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