简体   繁体   English

如何在 ngFor 中单击 angular 以突出显示选定的单选按钮

[英]How to highlight the selected radio button with on click in angular inside ngFor

component.ts组件.ts

    //all imports are done
    
ngOnInit() {
      this.list = {
          'eatList': [{
            'class': 'Fruits',
            'color': ['Red', 'White', 'Black'],
            'imageSrc': ['/assets/images/fruit/red-fruit.png', 
              '/assets/images/fruit/black-fruit.png',
              '/assets/images/fruit/white-fruit.png'],
             'weights' : [60, 50]
          },
          {
            'modalName': 'Vegetable',
            'color': ['Green', 'Black'],
            'imageSrc': ['/assets/images/veg/black-veg.png', /assets/images/veg/green-veg.png'],
            'weights' : [40, 50]

}

component.html组件.html

<div>
  <div *ngFor="let eats of list.eatList" "
   >
    <h3>{{eats.class}}</h3>
    <img src="{{eats.imageSrc[0]}}" alt="image" />

    
    
    <div>
         <ul>
      <li *ngFor="let weight of eats.weights;let i = index;">
        <input type="radio" [value]="weight" (change)="handleChange($event)" [attr.id]="i" 
          name="weight" />
        <label for="{{i}}">{{weight}}</label>
      </li>
    </ul>
    </div>
  </div>
</div>

When I click on the input radio button I need the particular item to be selected in some color.当我单击输入单选按钮时,我需要以某种颜色选择特定项目。 added the change event but unable to change it.添加了更改事件但无法更改它。

You can access the element by using the event passed to the function, since it's a click event, the event has a target, which is the input element, so you can do something like:您可以使用传递给 function 的事件来访问该元素,因为它是一个单击事件,所以该事件有一个目标,即输入元素,因此您可以执行以下操作:

handleChange(event) {
    event.target.style.color = "yellow";
}

This change won't be noticeable, I just gave it as an example, since you didn't specify what does the following means:此更改不会引起注意,我只是将其作为示例,因为您没有指定以下含义是什么:

particular item to be selected in some color以某种颜色选择的特定项目

If you want to change the div for the eats element, you can do something like the following on the template:如果要更改eats元素的div,可以在模板上执行以下操作:

<div>
  <div *ngFor="let eats of list.eatList" [style.background]="eats.backgroundColor"
   >
    <h3>{{eats.class}}</h3>
    <img src="{{eats.imageSrc[0]}}" alt="image" />

    
    
    <div>
         <ul>
      <li *ngFor="let weight of eats.weights;let i = index;">
        <input type="radio" [value]="weight" (change)="handleChange($event, eats)" [attr.id]="i" 
          name="weight" />
        <label for="{{i}}">{{weight}}</label>
      </li>
    </ul>
    </div>
  </div>
</div>

and in your component:并在您的组件中:

handleChange(event, parent) {
  parent.backgroundColor = "yellow";
}

I've made an assumption here that eats is an object.我在这里假设吃的是 object。 Also, you can change the binding to be for a class (using ngClass) and make a lot of css changes that way此外,您可以将绑定更改为 class(使用 ngClass)并以这种方式进行大量 css 更改

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

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