首页 > 解决方案 > Angular 9 改变元素的属性值

问题描述

我正在尝试更改元素的属性值,但未在元素中设置新值

Angular 的任何最佳标准实践,通过它我们可以解决以下问题

 <div name="hai"  (click)=test($event)  #ckechName>

在 Ts 文件中

test(event){

let exmpl=event.currentTarget
exmpl.getAttribute('name')  //I am able to retrieve this valu as hai

exmp.setAttribute('name','hello')  //This is value is not setting as hello in the dom..It is still showing hai

}

标签: htmlangulartypescript

解决方案


试试这个解决方案

  @ViewChild('ckechName', {static: true}) myCkechName:ElementRef;

在您的功能中test,您可以这样做

this.myCkechName.nativeElement.value = 'hello';

您不需要传入$event测试功能

希望有用


推荐阅读