【问题标题】:Get a data from app-root to component从 app-root 获取数据到组件
【发布时间】:2017-11-15 16:55:29
【问题描述】:
<body>
  <app-root data="myData"></app-root>
</body>

我需要检索myData,我将手动添加到index.html 并将其用于组件。

如何检索这些数据?

谢谢。

【问题讨论】:

标签: angular


【解决方案1】:

我相信这个答案有你所需要的:

https://stackoverflow.com/a/43544999/3581932

使用@Attribute(对于 Angular 4)

index.html:

<my-app myAttribute="myAttributeValue">
    <div>Loading...</div>
</my-app>

app.component.ts :

@Component({
    selector: 'my-app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css']
})
export class AppComponent {
    constructor(private elementRef:ElementRef) {
        let myAttribute = this.elementRef.nativeElement.getAttribute('myAttribute');
    }
}

[直接从上面的帖子复制]

【讨论】:

    猜你喜欢
    • 2018-07-18
    • 2017-07-20
    • 2013-04-25
    • 1970-01-01
    • 2020-03-31
    • 2017-06-24
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多