我对角度很新,我想使用鼠标悬停事件来更改src值。我们怎样才能在角度4或以上的版本中做到这一点?请帮帮我。
{{1}}
答案 0 :(得分:2)
让我们来看看如何使用模型而不是重写元素属性:
<img [src]="imgSrc"
(mouseover)="imgSrc = 'http://www.impresabaraldo.it/Blog/wp-content/uploads/2015/05/casa-ecologica-vicenza.jpg'"
(mouseout)="imgSrc = 'https://www.ediltecnico.it/wp-content/uploads/2017/06/casa-300x223.png'">
解决问题的可能方法可能是这样的:
export const ROUTES: RouteInfo[] = [
{ path: '/dashboard', title: 'Dashboard', icon: 'fa fa-tachometer', class: '' ,name:'',src: '', srcOut: "../../../../assets/images/header-icon.png",srcOn:"../../../../assets/images/dashboard_highlighted.png"},
{ path: '/forms', title: 'Forms', icon:'fa fa-eyedropper', class: '',name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/tables', title: 'Desk Blotter', icon:'fa fa-pencil', class: '' ,name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/bs-element', title: 'Admin', icon:'fa fa-picture-o', class: '' ,name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/login', title: 'Logout', icon:'fa fa-power-off', class: '',name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:"" },
];
请注意,我们有一个src属性,一个srcOn和一个srcOut。想法是将图像src属性绑定到此json对象src属性,然后根据鼠标事件更新json对象src属性
<li *ngFor="let menuItem of menuItems" routerLinkActive="active" class="{{menuItem.class}}" >
<a [routerLink]="[menuItem.path]" name="{{menuItem.name}}">
<img style="width:66%;" id="{{menuItem.title}}"
[src]="menuItem.src || menuItem.srcOut"
(mouseover)="menuItem.src = menuItem.srcOn"
(mouseout)="menuItem.src = menuItem.srcOut"/> {{menuItem.title}}
</a>
</li>
最后,我建议您使用mouseover而不是mouseenter