javascript - 更改按钮值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 编写应用程序。
解决方案
你的问题太宽泛了,因为你显然还没有尝试过任何东西,但它看起来像这样:
<button [innerHTML]="myComponentVariableName"></button>
然后,在您的函数中,您将有条件地渲染图像:
let myComponentVariableName = '';
if (something) {
myComponentVariableName = '<img ... />';
} else {
myComponentVariableName = '<img ... />';
}
您也可以将图像放入标记中,然后更改源属性。
<button><img [src]="myComponentVariableName" /></button>
推荐阅读
- android - 三星体验版
- python - Python,Unittest + Pandas DataFrames:这个断言测试 = 一个布尔值(没有得到 ValueError)吗?
- inheritance - Kotlin Companion 对象关于反射和继承的限制
- dataweave - Dataweave 2.0:将嵌套数组转换为具有父详细信息的平面列表
- python - AttributeError:模块“pyproj”没有属性“pyproj_datadir”
- c# - 在每个端点的基础上启用特定的身份验证/授权方案
- javascript - loader 的 this._compilation 的替代方案
- mysql - 我该如何解决这个mysql存储函数问题?
- r - 在 R 中拆分大型数据集的有效方法
- javascript - 循环遍历对象列表未正确显示