首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问angular2中的数据集映射

访问angular2中的数据集映射
EN

Stack Overflow用户
提问于 2016-01-17 22:55:48
回答 1查看 777关注 0票数 1

我有以下几点:

.html模板

代码语言:javascript
复制
<name-view data-topic='id'></name-view>

.dart组件

代码语言:javascript
复制
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中检索数据主题属性?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-19 10:32:38

我没能让@Input('data-topic')发挥作用,但其他方法正在起作用

代码语言:javascript
复制
@Component(
    selector: 'app-element',
    template: '''
<h1>app-element</h1>
<name-view data-topic='id' topic='topicid'></name-view>
''',
    directives: const [NameView])
class AppElement {}
代码语言:javascript
复制
@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或服务器呈现中运行代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34844777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档