首页 > 解决方案 > 更改按钮值Angular 7

问题描述

一个问题:

<button (click)="activateMotion(1)">
    <img class="emotion-icon" id="positive-icon" src="" />
</button>
<button (click)="activateMotion(-1)">
    <img class="emotion-icon" id="negative-icon" src="" />
</button>

我有两个按钮,其值为图像。我想写一个函数调用:activateMotion()。我想根据值 1 或 -1 更改按钮内的图像。我可以使用 jQuery 来实现这里的目标,但我正在用 Angular 编写应用程序。

标签: javascriptjqueryangularangular6angular7

解决方案


你的问题太宽泛了,因为你显然还没有尝试过任何东西,但它看起来像这样:

<button [innerHTML]="myComponentVariableName"></button>

然后,在您的函数中,您将有条件地渲染图像:

let myComponentVariableName = '';

if (something) {
    myComponentVariableName = '<img ... />';
} else {
    myComponentVariableName = '<img ... />';
}

您也可以将图像放入标记中,然后更改源属性。

<button><img [src]="myComponentVariableName" /></button>

推荐阅读