在 angular2 中访问数据集地图
Accessing a dataset map in angular2
我有以下内容:
.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 中的数据主题属性?
我无法使 @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 或服务器呈现中的代码。
我有以下内容:
.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 中的数据主题属性?
我无法使 @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 或服务器呈现中的代码。