2016-06-28 366 views
0

我看了一些答案,但没有工作对我来说,我输了...我用科尔多瓦,离子和角2对象不支持属性或方法“的getAttribute”

这里是我的HTML

<ion-col *ngFor="let value of myButtonsFirstRow" width-25><button #elem ptcolor (click)="volumeClick(elem)" attr.floatValue="{{value}}"> {{value | fraction}}</button></ion-col> 

这里是我的打字稿

volumeClick(htmlElem: HTMLElement) { 
    this.volumeSelected = +htmlElem.getAttribute('floatValue'); 
} 

我想设置从我创建的属性值(在我的HTML)。但看起来像我不能使用getAttribute方法。我的htmlElem已设置,但我没有看到该方法,所以我知道我做错了什么,但我不知道是什么!

由于

回答

1

是的,那是因为elem是一种离子组分。所以我看到几种方法来做到这一点:

1)不幸的是Button组件没有公共方法/财产来获取nativeElement/attribute。此代码应工作:

volumeClick(elem: any) { 
    this.volumeSelected = +elem._elementRef.nativeElement.getAttribute('floatValue'); 
} 

2)获得属性另一种方式是在你的模板使用$event这样的:

<button (click)="volumeClick($event)" attr.floatValue="{{value}}"> {{value}}</button> 

然后你的方法是这样的:

volumeClick(event: any) { 
    this.volumeSelected = +event.currentTarget.getAttribute('floatValue'); 
} 

注意:我使用currentTarget,因为Button组件有几个孩子。

3)你可以只通过value点击事件:

<button (click)="volumeClick(value)">{{value}}</button> 

volumeClick(value: any) { 
    this.volumeSelected = value; 
} 

还是有点简单:

<button (click)="volumeSelected = value">{{value}}</button> 
+0

这就是我认为的离子元素...即使按钮元素与HTML相同哈哈。无论如何,我不会再为那一个而堕落。你的答案#1是完美的!再次感谢 ! :) – theyouishere

0

模板变量的基准返回ElementRef。如果您要访问的HTMLElement使用ElementRef.nativeElement

volumeClick(htmlElem: ElementRef) { 
    this.volumeSelected = +htmlElem.nativeElement.getAttribute('floatValue'); 
} 
+0

你不觉得'attr.floatValue'应与'[包装] ' –

+2

无论是'[]'还是'{{}}'但都不是。 –

+0

这是我尝试的第一件事情之一。它的工作,我有访问getAttribute方法。但是,它并不像我没有得到什么属性。我是否在getAttribute中使用了良好的语法? – theyouishere

相关问题