【发布时间】:2020-10-26 10:46:16
【问题描述】:
这是一个简单的问题,但我无法解决它,因为我是 Angular 和 Web 开发的新手。基本上有两个组件主页和仪表板。 home.component.html 中的按钮将图像的来源从bulbOn.png 更改为bulbOff.png。我希望同一个按钮也应该在dashboard.component.html上类似地更改源。我想我需要为此使用打字稿,但我不知道如何。基本上一个html上的onClick应该如何对另一个html执行操作?
home.component.html
<mat-card >
<button onclick="document.getElementById('myImage').src='assets/BulbOn.svg'">Turn on the bulb.</button>
<img id="myImage" src="assets/BulbOn.svg" style="width:100px">
<button onclick="document.getElementById('myImage').src='assets/BulbOff.svg'">Turn off the bulb.</button>
</mat-card>
dashboard.component.html
<mat-card class="bulbCard">
<div class="bulbimg"> <img src="assets/BulbOn.svg"> </div>
</mat-card>
dashboard.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.less']
})
export class DashboardComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
home.component.ts
import { Component } from '@angular/core';
import { User } from '@app/_models';
import { AccountService } from '@app/_services';
@Component({ templateUrl: 'home.component.html',
styleUrls: ['./home.component.less'] })
export class HomeComponent {
user: User;
constructor(private accountService: AccountService) {
this.user = this.accountService.userValue;
}
}
【问题讨论】:
标签: javascript html css angular typescript