我有以下几点:
.html模板
<name-view data-topic='id'></name-view>.dart组件
import 'dart:html';
class NameView implements AfterViewInit
{
String topic;
void ngAfterViewInit( ) {
// datset is not reference below - works in polymer-1.x attached() method
topic = this.datset['topic'];
}
}但是,ngAfterViewInit中的代码不起作用。如何在angular2中检索数据主题属性?
发布于 2016-01-19 10:32:38
我没能让@Input('data-topic')发挥作用,但其他方法正在起作用
@Component(
selector: 'app-element',
template: '''
<h1>app-element</h1>
<name-view data-topic='id' topic='topicid'></name-view>
''',
directives: const [NameView])
class AppElement {}@Component(
selector: 'name-view',
template: '''
<h1>test-element</h1>
<div>data-topic: {{topic}}</div>
<div>topic: {{topic2}}</div>
<div>topicFromRef: {{topicFromRef}}</div>
''')
class NameView implements OnInit {
@Input('data-topic')
String topic;
@Input('topic')
String topic2;
String topicFromRef;
ElementRef _elementRef;
TestElement(this._elementRef);
void ngOnInit() {
topicFromRef = (_elementRef.nativeElement as Element).dataset['topic'];
}
}不鼓励使用ElementRef,因为这样可以防止在WebWorker或服务器呈现中运行代码。
https://stackoverflow.com/questions/34844777
复制相似问题