Javascript 单击时角度更新父div类

Javascript 单击时角度更新父div类,javascript,html,css,angular,Javascript,Html,Css,Angular,我有以下代码,我希望在单击图像时更新父类。单击时,图像将调用“SelectVariation”方法。有没有办法做到这一点 component.html: <div class="clr-row"> <ng-container *ngFor="let variOption of product.variOptions"> <div class="card clickable clr-col-2 variationCard" *ngFor="let variOptio

我有以下代码,我希望在单击图像时更新父类。单击时,图像将调用“SelectVariation”方法。有没有办法做到这一点

component.html:

<div class="clr-row">
<ng-container *ngFor="let variOption of product.variOptions">
<div class="card clickable clr-col-2 variationCard" 
*ngFor="let variOptionTwo of variOption.variOptionTwos"> //Update this class
  <div class="card-img" (click)="selectVariation(variOptionTwo.id, $event)">
    <clr-tooltip>
      <img src="{{variOptionTwo.url}}" class="variationImgScale" clrTooltipTrigger>
      <clr-tooltip-content clrPosition="top-right" clrSize="m" *clrIfOpen>
          <span>{{variOption.optName}} - {{variOptionTwo.optName}}</span>
      </clr-tooltip-content>
    </clr-tooltip>
  </div>
</div>

在子组件中使用
@Output()variableHere=neweventemitter();
this.variableHere.emit(this.variableToSend);

然后在父模板中,将变量与html子模板定义中的方法关联:

在父组件中,定义方法并执行与方法结果相等的变量,例如:

manageVariable(event) {
   this.variableToUpdate = event;
 }

如果必须检查变量是否已更改其状态,请调用需要在ngDoCheck()中检查的内容。

在子组件中使用
@Output()variableHere=neweventemitter();
this.variableHere.emit(this.variableToSend);

然后在父模板中,将变量与html子模板定义中的方法关联:

在父组件中,定义方法并执行与方法结果相等的变量,例如:

manageVariable(event) {
   this.variableToUpdate = event;
 }

如果必须检查变量是否已更改其状态,请调用ngDoCheck()中需要检查的内容。

利用angular with
output中的
EventEmitter

parent.component.html

<my-child-comp (onSelectVariation)="myVariation($event)" ></my-child-comp>
<button (click)="onSelectVariation.emit(myData)">trigger variation</button>
child.component.html

<my-child-comp (onSelectVariation)="myVariation($event)" ></my-child-comp>
<button (click)="onSelectVariation.emit(myData)">trigger variation</button>

您在
输出中定义的名称应用作父级中的it主机元素中的事件

利用
事件发射器
输出

parent.component.html

<my-child-comp (onSelectVariation)="myVariation($event)" ></my-child-comp>
<button (click)="onSelectVariation.emit(myData)">trigger variation</button>
child.component.html

<my-child-comp (onSelectVariation)="myVariation($event)" ></my-child-comp>
<button (click)="onSelectVariation.emit(myData)">trigger variation</button>

您在
输出中定义的名称
应该用作父级中it主机元素中的事件

您的意思是什么?你是说有一个父组件吗?如果是这样,请检查EventEmitter@iamjc015,对不起,我应该让我的标题更具体。我的意思是更新父DIV,而不是父组件。在我的component.html中,我想用“card img”类在div上单击时更新类为“card clickable”的父div。这是什么意思?你是说有一个父组件吗?如果是这样,请检查EventEmitter@iamjc015,对不起,我应该让我的标题更具体。我的意思是更新父DIV,而不是父组件。在my component.html中,我想在使用“card img”类单击div时更新类为“card clickable”的父div。感谢您的回答,但很抱歉,我应该让我的标题更具体。我的意思是更新父DIV,而不是父组件。在my component.html中,我想用“card img”类更新父div,当单击div时,该父div的类为“card clickable”。您想在父元素中添加/删除什么类?我有一个名为“selected”的CSS类,它将在单击时为框应用边框。谢谢您的回答,但很抱歉,我应该让我的标题更具体。我的意思是更新父DIV,而不是父组件。在my component.html中,我想用“card img”类更新父div,当单击div时,该父div的类为“card clickable”。您想在父元素中添加/删除什么类?我有一个名为“selected”的CSS类,它将在单击时为框应用边框。谢谢您的回答,但很抱歉,我应该让我的标题更具体。我的意思是更新父DIV,而不是父组件。在my component.html中,我想在使用“card img”类单击div时更新类为“card clickable”的父div。然后使用ngModel对div使用变量值谢谢您的回答,但很抱歉,我应该使我的标题更具体。我的意思是更新父DIV,而不是父组件。在my component.html中,我想用“card img”类单击div时更新类为“card clickable”的父div。然后使用变量值对div使用ngModel