首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从按钮角度4显示图像

如何从按钮角度4显示图像
EN

Stack Overflow用户
提问于 2018-03-16 10:09:27
回答 2查看 8.9K关注 0票数 0

所以我试着做一个基本的网站,当我点击一个按钮时,我需要显示一个图像。问题是,它显示了图像,但是当我单击另一个按钮来显示另一个图像时,前面的图像就会保留下来。这是我的密码。我用的是角4和打字稿。

component.ts

代码语言:javascript
复制
openPredictionImg() {
const myImg = new Image();
myImg.useMap = '../../assets/prediction.png';
const img = document.createElement('img');
document.body.appendChild(img);
img.setAttribute('src', myImg.useMap);
img.onload = (stop);}

openRebalancingImg() {
const myImg = new Image();
myImg.useMap = '../../assets/rebalancing.png';
const img = document.createElement('img');
document.body.appendChild(img);
img.setAttribute('src', myImg.useMap);
img.onload = (stop);}

openVisualizationImg() {
const myImg = new Image();
myImg.useMap = '../../assets/visualization.png';
const img = document.createElement('img');
document.body.appendChild(img);
img.setAttribute('src', myImg.useMap);
img.onload = (stop);}

component.html

代码语言:javascript
复制
<button class="predictionBtn "(click)="openPredictionImg()" style="width: 10%">Prediction</button>

<button class="rebalancingBtn" (click)="openRebalancingImg()" style="width: 10%">Rebalancing</button>

<button class="visualizationBtn" (click)="openVisualizationImg()" style="width: 10%">Visualization</button>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-16 13:37:44

好的,这是解决办法

component.ts

代码语言:javascript
复制
text = '';
openPrediction() {
this.text = '...' }

component.html

代码语言:javascript
复制
<button class="predictionBtn " (click)="imageSource = '../../assets/prediction.png'; openPrediction()" >Prediction</button>
<img [src]="imageSource" *ngIf="imageSource" style="width: 10%"/>
{{ text }}
票数 0
EN

Stack Overflow用户

发布于 2018-03-16 10:15:58

在角度上,你不能直接接触DOM。

我能想到的最简单的方法是:

代码语言:javascript
复制
<button class="predictionBtn "(click)="imageSource = '../../assets/prediction.png'" style="width: 10%">Prediction</button>

<button class="rebalancingBtn" (click)="imageSource = '../../assets/rebalancing.png'" style="width: 10%">Rebalancing</button>

<button class="visualizationBtn" (click)="imageSource = '../../assets/visualization.png'" style="width: 10%">Visualization</button>

<img [src]="imageSource" *ngIf="imageSource"/>

在您的类型记录中,删除所有以前的代码,只需声明一个变量。

代码语言:javascript
复制
imageSource: string;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49318045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档