gpt4 book ai didi

html - 如何更改属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:41 24 4
gpt4 key购买 nike

我有一个具有一组三个自定义按钮的组件,我想将这些按钮用作录音机的控件。

我卡在了第一阶段,我想根据按钮的功能更改按钮上显示的符号。

我试图通过更改它们的 xlink:href 属性(我使用 svg)来实现这一点,但在他的控制台中得到了这个:

EXCEPTION: Error in ./RecordComponent class RecordComponent - inline template:5:45 caused by: this.roundButtonOne.getAttribute is not a function

知道为什么以及如何使用 Angular 方法实现它吗?这是代码:

import {Component, ViewChild} from '@angular/core';

@Component({
selector: 'app-record',
template: `
<svg class='roundButtonOne icon'>
<use #roundButtonOne xlink:href='#mic'(click)='onRecord()'/>
</svg>
<svg class='roundButtonTwo icon'>
<use #roundButtonTwo xlink:href='#live' />
</svg>
<svg class='roundButtonThree icon'>
<use #roundButtonThree xlink:href='#upload' />
</svg>
`
})
export class RecordComponent {

private recording: boolean = false;

@ViewChild('roundButtonOne') roundButtonOne: HTMLElement;
@ViewChild('roundButtonTwo') roundButtonTwo: HTMLElement;
@ViewChild('roundButtonThree') roundButtonThree: HTMLElement;

onRecord() {
this.recording = true;

switch(this.roundButtonOne.getAttribute('xlink:href')) {
case '#mic':
this.record();
break;
}
}

record() {
this.roundButtonOne.setAttribute('xlink:href','#mic-small');
this.roundButtonTwo.setAttribute('xlink:href', '#pause');
this.roundButtonThree.setAttribute('xlink:href', '#stop');
}
}

最佳答案

如果您在其中一个按钮元素上调用 console.log,您可以看到它是 ElementRef 的一个实例,而不是 HTMLElement .

所以...

从@angular/core 导入 ElementRef:

import {Component, ViewChild, ElementRef} from '@angular/core';

将按钮类型从 HTMLElement 更改为 ElementRef:

@ViewChild('roundButtonOne') roundButtonOne: ElementRef;
@ViewChild('roundButtonTwo') roundButtonTwo: ElementRef;
@ViewChild('roundButtonThree') roundButtonThree: ElementRef;

ElementRef 对象获取 nativeElement 然后调用 setAttribute()/getAttribute() 方法:

this.roundButtonOne.nativeElement.getAttribute('xlink:href');

this.roundButtonOne.nativeElement.setAttribute('xlink:href','#mic-small');

关于html - 如何更改属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42097796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com