gpt4 book ai didi

dart - 在angular2中访问数据集映射

转载 作者:行者123 更新时间:2023-12-03 04:05:26 25 4
gpt4 key购买 nike

我有以下内容:

.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中检索data-topic属性?

最佳答案

我无法使@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或服务器渲染中运行代码。

关于dart - 在angular2中访问数据集映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34844777/

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